如何从python sh打开windows文件

2024-04-24 21:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Copy comment:

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

Tags: 文件intesttxt教程openusers技术
2条回答
file_open = open('C:\\Users\\User\\Documents\\test.txt')

这就可以了,你需要用\来转义特殊字符,比如\

如果打开是指加载文件:

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

相关问题 更多 >