【问题标题】:How to open a windows file from a python shell如何从python shell打开一个windows文件
【发布时间】:2019-04-06 12:16:29
【问题描述】:

我是 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

【问题讨论】:

  • "FileNotFoundError:":您的文件 'Arrows' 的扩展名可能类似于 '.JPG',或者不在当前工作目录中。
  • 是的,它有一个扩展名“.py”。但它在当前工作目录中。
  • 感谢您的观察,我现在已经解决了问题

标签: python path python-unicode


【解决方案1】:
file_open = open('C:\\Users\\User\\Documents\\test.txt')

这应该可以解决问题,您需要使用\ 转义\ 等特殊字符

【讨论】:

    【解决方案2】:

    如果打开是指加载文件:

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

    【讨论】:

      猜你喜欢
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多