Python中意外的“没有这样的文件或目录”

2024-09-29 19:35:46 发布

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

我将文件移动到cwd的两行代码如下:

import os, shutil
shutil.move(os.path.abspath('hello_file.txt'),os.getcwd())

我确实有cwd上的一个文件,但我得到了这个回溯:

Traceback (most recent call last):

  File "<ipython-input-333-f012757a9dca>", line 1, in <module>
    shutil.move(os.path.abspath('hello_file.txt'),os.getcwd())

  File "/Users/deepayanbhadra/anaconda3/lib/python3.6/shutil.py", line 558, in move
    copy_function(src, real_dst)

  File "/Users/deepayanbhadra/anaconda3/lib/python3.6/shutil.py", line 257, in copy2
    copyfile(src, dst, follow_symlinks=follow_symlinks)

  File "/Users/deepayanbhadra/anaconda3/lib/python3.6/shutil.py", line 120, in copyfile
    with open(src, 'rb') as fsrc:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/deepayanbhadra/Desktop/DIY-Python-Projects/Automating Tasks/hello_file.txt'

Tags: inpysrctxthellomoveoslib
1条回答
网友
1楼 · 发布于 2024-09-29 19:35:46

您提供的文件路径是当前目录。你首先要换到目录上去。你可以这样做的方法之一是

import os 
os.chdir(“..”)

这是最简单的方法,但需要大量代码才能实现

相关问题 更多 >

    热门问题