FilenotFoundError即使需要在重命名之前通过条件

发布于 2025-02-01 21:09:21 字数 1487 浏览 3 评论 0原文

我正在编写一个脚本,该脚本应该自动化大量PDF文件的移动和重命名。这些文件最初是在临时目录中下载的,最初下载的路径将保存在JSON文件中。文件肯定存在于我在脚本中指示的源位置中,甚至使用相同的变量,这些变量通过在尝试重命名之前检查文件存在的条件,我仍然会得到filenotfounderror

我的代码:


def distribute(source, title_journal, year_of_issue, volume_and_issue, title): # title_journal, year_of_issue and volume_and_issue are just strings of directory names

    root_destination = os.path.join(package_path, "Downloads") # package_path = working directory (os.getcwd()), "Downloads" = parent directory where files will be moved
    journal_destination = os.path.join(root_destination, title_journal) 
    year_destination = os.path.join(journal_destination, year_of_issue)
    file_destination = os.path.join(volume_destination, title)
    volume_destination = os.path.join(year_destination, volume_and_issue)
    current_path_to_file = source
    file_destination = os.path.join(volume_destination, f”{title}.pdf”)
    os.rename(current_path_to_file, file_destination)


current_path = Path(dictionary["Download_path"]) # is an absolute path to the file's current location
    if current_path.is_file():
        distribute(current_path, journal, year, volume, next(iter(dictionary))) 

错误的示例(编辑以在我的脚本中显示变量):

FileNotFoundError: [Errno 2] No such file or directory: '/Users/me/Documents/run1/Downloads/Temporary_folder/09567976211043428.pdf' -> '/Users/me/Documents/Downloads/journal_title/year_of_issue/volume_and_issue/new_file_name'

I am writing a script that should automate the moving and renaming of a huge amount of pdf files. The files are initially downloaded in a temporary directory, the path where they were initially downloaded is saved in a json file. The files definitely exist in the source location I indicate in the script, I even use the same variables that pass the condition that checks that the file exists before trying to rename, and I still get a FileNotFoundError .

My code:


def distribute(source, title_journal, year_of_issue, volume_and_issue, title): # title_journal, year_of_issue and volume_and_issue are just strings of directory names

    root_destination = os.path.join(package_path, "Downloads") # package_path = working directory (os.getcwd()), "Downloads" = parent directory where files will be moved
    journal_destination = os.path.join(root_destination, title_journal) 
    year_destination = os.path.join(journal_destination, year_of_issue)
    file_destination = os.path.join(volume_destination, title)
    volume_destination = os.path.join(year_destination, volume_and_issue)
    current_path_to_file = source
    file_destination = os.path.join(volume_destination, f”{title}.pdf”)
    os.rename(current_path_to_file, file_destination)


current_path = Path(dictionary["Download_path"]) # is an absolute path to the file's current location
    if current_path.is_file():
        distribute(current_path, journal, year, volume, next(iter(dictionary))) 

Example of Error (edited to show variables in my script):

FileNotFoundError: [Errno 2] No such file or directory: '/Users/me/Documents/run1/Downloads/Temporary_folder/09567976211043428.pdf' -> '/Users/me/Documents/Downloads/journal_title/year_of_issue/volume_and_issue/new_file_name'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2025-02-08 21:09:21

假设/user/me/documents/downloads已经存在,错误是在告诉您,该目录中的一个或多个目录jeramy_title/year_of_issue/lay_of_issue/volume_and_issue不存在,并且不会隐含地创建。

通常的解决方案是用os.makedirs nocation_ok = true 确保存在目标目录结构。

    file_destination = os.path.join(volume_destination, f"{title}.pdf")
    os.makedirs(volume_destination, exist_ok=True) # <------
    os.rename(current_path_to_file, file_destination)

切线,还请注意,我必须将卷曲的“印刷”双引号更改为适当的ASCII双引号。

Assuming /Users/me/Documents/Downloads already exists, the error is telling you that one or more of the directories journal_title/year_of_issue/volume_and_issue within that directory do not exist, and will not be created implicitly.

The usual solution is to call os.makedirs with exist_ok=True to make sure the destination directory structure exists.

    file_destination = os.path.join(volume_destination, f"{title}.pdf")
    os.makedirs(volume_destination, exist_ok=True) # <------
    os.rename(current_path_to_file, file_destination)

Tangentially, notice also that I had to change the curly "typographer" double quotes to proper ASCII double quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文