user1551817
user1551817

Reputation: 7451

unwanted \n after reading in lines

I am trying to change directories via python, but the folder name is variable (chosen from a list):

f=open('folderlist.txt')
for line in f:

    pname = line

    os.chdir('./P574/%s' % (pname))

Which doesn't quite work because I get the error message: "No such file or directory: './folders/folder_name2\n'"

The folder names I want are in a list called "folderlist.txt", but how do I stop python adding the '\n' at the end?

Thank you!

Upvotes: 0

Views: 166

Answers (1)

halex
halex

Reputation: 16393

You have to use rstrip. Change the line pname = line to

pname = line.rstrip('\n')

Upvotes: 2

Related Questions