文件读取和写入时,文件分隔符的差别

发现python中Image和fle在打开文件时,文件分隔符的三种写法都支持,即如下都是可以的

img = Image.open("C:\QQ图片.jpg")
img = Image.open("C:\\QQ图片.jpg")
img = Image.open("C:/QQ图片.jpg")

file("d:/python_file.txt", "w")
file("d:\python_file.txt", "w")
file("d:\\python_file.txt", "w")


但是在Image在写文件的时候确只支持以下两种:

new_img.save("C:\\Users\\Administrator\\Desktop\\new_img.jpg")
new_img.save("C:/Users/Administrator/Desktop/new_img.jpg")

这种运行无法通过:
# new_img.save("C:\Users\Administrator\Desktop\new_img.jpg")


看来在写文件分隔符的时候,最好还是统一都用“\\”,如下处理一下:replace("/", "\\"),replace("\", "\\")

你可能感兴趣的:(Python)