我正在尝试用单正斜杠更改路径分隔符的双正斜杠。该程序读取一个包含路径的文件列表的文本文件。我也在使用一个windows盒子。
f = open('C:/Users/visc/scratch/scratch_child/test.txt')
destination = ('C:/Users/visc')
# read input file line by line
for line in f:
line = line.replace("\\", "/")
#split the drive and path using os.path.splitdrive
(drive, path) = os.path.splitdrive(line)
#split the path and fliename using os.path.split
(path, filename) = os.path.split(path)
#print the stripped line
print line.strip()
#print the drive, path, and filename info
print('Drive is %s Path is %s and file is %s' % (drive, path, filename))
和:
line = line.replace("\\", "/")
它工作正常,但不是我想要的。但是如果我用反斜杠替换正斜杠,我会收到语法错误