FilenotFoundError即使需要在重命名之前通过条件
我正在编写一个脚本,该脚本应该自动化大量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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
/user/me/documents/downloads
已经存在,错误是在告诉您,该目录中的一个或多个目录jeramy_title/year_of_issue/lay_of_issue/volume_and_issue
不存在,并且不会隐含地创建。通常的解决方案是用
os.makedirs
nocation_ok = true 确保存在目标目录结构。切线,还请注意,我必须将卷曲的“印刷”双引号更改为适当的ASCII双引号。
Assuming
/Users/me/Documents/Downloads
already exists, the error is telling you that one or more of the directoriesjournal_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
withexist_ok=True
to make sure the destination directory structure exists.Tangentially, notice also that I had to change the curly "typographer" double quotes to proper ASCII double quotes.