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

问题描述 投票:0回答:2

我是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
python path python-unicode
2个回答
0
投票
file_open = open('C:\\Users\\User\\Documents\\test.txt')

这应该可以解决问题,你需要使用\来逃避像\这样的特殊字符


0
投票

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

path = "C:/Users/User/Documents/test.txt"
file = open(path, 'r')
© www.soinside.com 2019 - 2024. All rights reserved.