繁体   English   中英

如何从python shell打开一个Windows文件

[英]How to open a windows file from a python shell

我是python的新手。 我已经尝试了教程和教科书中可用的所有技术,通过python shell打开一个已经存在的文件,它总是返回一种形式的错误或另一种形式。

test_file = open('C:\Users\User\Documents\test.txt')

复制评论

 Traceback (most recent call last): File "<pyshell#48>", line 1, in <module> see_file = open('Arrows') FileNotFoundError: [Errno 2] No such file or directory: 'Arrows' test_file = open('C:\\Users\\User\\Documents\\test.txt') SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXX escape 
file_open = open('C:\\Users\\User\\Documents\\test.txt')

这应该做的伎俩,你需要转义特殊字符,如\\ with \\

如果打开你的意思是加载一个文件:

path = "C:/Users/User/Documents/test.txt"
file = open(path, 'r')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM