我是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')
这应该可以解决问题,你需要使用\
来逃避像\
这样的特殊字符
如果打开你的意思是加载一个文件:
path = "C:/Users/User/Documents/test.txt"
file = open(path, 'r')