Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading filenames from file in python

Tags:

python

I'm reading filenames from a ascii file using

with open('FilenamesAsciiFile.txt') as f:
    content = f.readlines()

Unfortunately when I try to open the file or check wether it exists I get an error:

filename = content[0]
print filename
print type(filename)

--> myFile.h5
--> <type 'str'>


import os.path
os.path.isfile(filename)

--> False

When I hardcode the string everything is working fine

os.path.isfile('myFile.h5')

--> True

Is string the wrong input type for that function?

like image 645
mcExchange Avatar asked Jul 16 '26 11:07

mcExchange


1 Answers

You need to trim '\n' from lines:

filename = content[0].strip()
like image 108
Eugene Soldatov Avatar answered Jul 19 '26 01:07

Eugene Soldatov