문제

I wish to import a list of files ex:

'E:\\mytest\\test_00.txt'
'E:\\mytest\\test_01.txt'
'E:\\mytest\\test_02.txt'


INPUT_txt = raw_input("Input File(s): ")
element = map(str, INPUT_txt.split(","))
for filename in element:
    print os.path.abspath(filename)
    print os.path.isfile(filename)

I got this result

E:\\mytest\\test_00.txt
True    
C:\PythonScript\ E:\\mytest\\test_01.txt
False    
C:\PythonScript\ E:\\mytest\\test_02.txt
False

only first file (test_00.txt) is True because located in the right directory

도움이 되었습니까?

해결책

You don't need map(str, INPUT_txt.split(",")) - the elements are already strings. Other than that, its just a matter of cleaning up the split filenames by stripping whitespace.

INPUT_txt = raw_input("Input File(s): ")
element = [ss for ss in (s.strip() for s in INPUT_txt.split(",")) if ss]
for filename in element:
    print os.path.abspath(filename)
    print os.path.isfile(filename)

다른 팁

Try this:

import os

INPUT_txt = raw_input("Input File(s): ")
element = map(str, INPUT_txt.split(","))
for filename in element:
    print filename
    print os.path.isfile(filename)

pay attention to the separator that you are using (in your case ","), for example a sequence of input:

Input file(s): ..\\file1.txt,C:\\file2.txt,results\\file3.txt

will generate a correct output, whereas:

Input file(s): ..\\file1.txt, C:\\file2.txt , results\\file3.txt

will not.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top