python - List to str adding backslash \ to string using str() -
i trying extract file paths txt file. file says c:\logs. use
with open(pathfile, "r") f: pathlist = f.readlines()
to produce list path in, , then
path1 = str(pathlist)
to produce line string. list sees line in th efile, te second command puts in backslash: c:\logs.
i
os.chdir(path1)
to @ path , error
windowserror: [error 123] filename, directory name, or volume label syntax incorrect: "['c:\\logs']"
why this? how can prevent it?
i looking have many paths in file , have script search each path individually. best way it?
thank much.
the backslash see "escape" character, how representation of string disambiguates existing backslash. it's not 2 backslashes
the problem pathlist
list
, , you're forcing str
. instead, take first element of pathlist:
path1 = pathlist[0]
you may have line break @ end (another use of escape: \n
or \r
). solve that, use .strip()
path1 = pathlist[0].strip()
Comments
Post a Comment