【问题标题】:Reading a file: No such file or directory读取文件:没有这样的文件或目录
【发布时间】:2021-09-10 15:13:59
【问题描述】:

我想读取我刚刚在我的目录中创建的 txt 文件 test_case_tranfromation.txt,但每当我尝试打开它时,我都会收到错误消息。

我试过了:

f = open("/Users/stanislavjirak/Documents/KBC_GROUP/rasa-cz/rasa_cz/rasa_extension/test/test_case_transformation.txt", "r")
print(f.readline())

with open("test_case_transformation.txt") as f:
    print(f.read())

两者都产生了:

FileNotFoundError: [Errno 2] 没有这样的文件或目录: 'test_case_transformation.txt'

我可以实际看到那里的文件,甚至可以复制$粘贴名称。

我做错了什么?

【问题讨论】:

  • 检查文件是否存在于当前python脚本所在的同一目录中。如果不是,则指定整个路径
  • 还要检查文件名。
  • 另外,如果你从 - C:/...开始你的路径会更好
  • @PCM 他在 macOS 上(可能)
  • 你可以去finder,找到你的文件,右键单击,按住键盘上的alt键,按Copy full path并将路径粘贴到python脚本中吗?

标签: python


【解决方案1】:

在 Python 中有两种获取文件的方法。要么使用文件的完整绝对路径,例如:“C:/Users/[username]/...”,要么提供相对于正在运行的 .py 文件的路径。例如,假设您的 .py 文件位于“C:/Users/[username]/file.py”,那么您可以访问同一目录中的 txt 文件,例如:“c:/Users/[用户名]/file.txt" 像这样:

with open("file.txt") as f:
    ...

【讨论】:

    【解决方案2】:

    如果您的文件位于根目录,则需要将文件名附加到当前工作目录。

    import os
    with open(os.getcwd() +"\\test_case_transformation.txt") as f:
    

    【讨论】:

      【解决方案3】:

      这个:

      with open("test_case_transformation.txt") as f:
          print(f.read())
      

      如果.py 文件和.txt 文件在同一目录中,则可以使用。否则,您应该将整个路径写入要打开的文件:

      with open("/path/to/file/test_case_transformation.txt") as f:
          print(f.read()) 
      

      如果你使用 Windows,我建议你这样做:

      with open(r"C:\path\to\file\test_case_transformation.txt") as f:
          print(f.read())
      

      【讨论】:

        【解决方案4】:

        当您收到文件未找到错误时,这肯定意味着该位置不存在文件。 如果您正在创建任何文件并在之后尝试读取它,请尝试使用相对路径进行写入和读取。 例如

            from pathlib import Path
        
            parents = Path(__file__).resolve().parents. // get all the parents of 
            current file location
            root_path = str(parents[0])
        
            //Create file
            with Path(root_path, "file_name.txt").open(mode="w") as fp:
               fp.write("some data")
        
            //read file
            with Path(root_path, "file_name.txt").open(mode="r") as fp:
              for line in fp:
                 ---do something-----
        

        这样您就不会丢失文件创建和读取的上下文。

        【讨论】:

          【解决方案5】:

          open("test_case_transformation.txt") 是相对于当前工作目录的。确保更改工作目录或使用完整路径。
          示例目录:

          /tmp
          ├── test_case_transformation.py
          └── test_case_transformation.txt
          

          test_case_transformation.py:

          import os
          print(os.path.exists('test_case_transformation.txt'))
          

          从 bash 调用:

          cd /tmp; python test_case_transformation.py                                                                       
          True
          
          cd /; python /tmp/test_case_transformation.py                                                                            
          False
          

          【讨论】:

            猜你喜欢
            • 2021-11-24
            • 2021-08-11
            • 1970-01-01
            • 1970-01-01
            • 2014-08-24
            • 2014-05-08
            • 1970-01-01
            • 2021-06-24
            • 2018-12-31
            相关资源
            最近更新 更多