Question

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

Was it helpful?

Solution

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)

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top