【问题标题】:FileNotFoundError [Errno 2] on os.getcwd() [duplicate]os.getcwd() 上的 FileNotFoundError [Errno 2] [重复]
【发布时间】:2018-03-19 16:11:48
【问题描述】:

我在 os.getcwd() 上收到 FileNotFoundError

def setUp(self):
    try:
        self.previous_dir=os.getcwd()
    except:
        print("no file?")
    try:
        self.test_dir.mkdir(parents=True, exist_ok=True)
        os.chdir(self.test_dir)
        self.logger.debug(f'CDed to {self.test_dir}')
    except (IOError, TypeError) as ioe:
        self.logger.error(f'Unable to make or CD to {self.test_dir}')                
        sys.exit(1)

我在 tearDown() 中 cd 回 self.previous_dir 并删除临时目录。

它是否试图访问已删除的文件?

【问题讨论】:

  • 你能添加完整的回溯吗?
  • 这可能是重复的,但我认为它更清楚。

标签: python python-3.x python-unittest


【解决方案1】:

答案是os.chdir() 被意外设置为已删除目录。该目录丢失了,但在尝试使用os.getcwd() 获取它时发生了错误(似乎)。

我用subprocess.run(['pwd'])调试过

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 2019-08-06
    • 1970-01-01
    • 2019-09-20
    • 2020-11-08
    • 2020-09-15
    • 1970-01-01
    • 2018-02-15
    • 2014-04-12
    相关资源
    最近更新 更多