Search code examples
pythoninputdirectoryraw-inputos.path

How to import a list of file in Python using raw_input


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


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)