简体   繁体   English

Python 错误 FileNotFoundError: [Errno 2] 没有这样的文件或目录

[英]Python Error FileNotFoundError: [Errno 2] No such file or directory

I'm trying to make code and save the csv file named as filename = "title" + "today's date and time" + ".csv"我正在尝试编写代码并保存名为 filename = "title" + "today's date and time" + ".csv" 的 csv 文件

from datetime import date
import csv

filename = "title_" + str(date.today()) + ".csv"
f = open(filename, "w", encoding="utf-8-sig", newline="")
writer = csv.writer(f)

this seem to be working and file is saved as "title_2020-12-03.csv"这似乎有效,文件保存为“title_2020-12-03.csv”

But I want to make file name with the time as well so I tried但我也想用时间制作文件名,所以我尝试了

from datetime import datetime
now = datetime.now()
today = now.strftime("%Y/%m/%d_%H:%M")

filename = "title_" + datetime.now().strftime("%Y/%m/%d_%H:%M") + ".csv"
f = open(filename, "w", encoding="utf-8-sig", newline="")
writer = csv.writer(f)

but this gives me an error但这给了我一个错误

FileNotFoundError: [Errno 2] No such file or directory: 'title_2020/12/03_11:22.csv' FileNotFoundError:[Errno 2] 没有这样的文件或目录:'title_2020/12/03_11:22.csv'

Any help would be appreciated, Thank you!任何帮助将不胜感激,谢谢!

The problem with your approach is that the required filename will be recognized as a modified filepath to your file because of the slashes ( '/' ).您的方法的问题是,由于斜杠( '/' ),所需的文件名将被识别为文件的修改文件路径。

So in case of the name 'title_2020/12/03_11:22.csv' you won't find a file with the same name in the directory relative to the cwd of the execution, but rather the program will interpret the filename as a path and will try to look up the file '03_11:22.csv' in the 'title_2020/12/' directory.因此,如果名称为'title_2020/12/03_11:22.csv' ,您将不会在相对于执行的 cwd 的目录中找到具有相同名称的文件,而是程序会将文件名解释为路径并将尝试在'title_2020/12/'目录中查找文件'03_11:22.csv'

Therefore you should use a different delimiter that doesn't confront with filepaths on your system.因此,您应该使用与系统上的文件路径不冲突的不同分隔符。 In general the safest is to use dashes or underscores.一般来说,最安全的是使用破折号或下划线。

Using dashes instead of slashes in the date it works:在它的工作日期中使用破折号而不是斜杠:

from datetime import datetime
now = datetime.now()
today = now.strftime("%Y/%m/%d_%H:%M")

filename = "title_" + str(datetime.now().strftime("%Y-%m-%d_%H:%M")) + ".csv"
f = open(filename, "w", encoding="utf-8-sig", newline="")
writer = csv.writer(f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FileNotFoundError: [Errno 2] 没有这样的文件或目录(python 错误) - FileNotFoundError: [Errno 2] No such file or directory (python error) Python: FileNotFoundError: [Errno 2] No such file or directory 错误 - Python: FileNotFoundError: [Errno 2] No such file or directory error FileNotFoundError: [Errno 2] 没有这样的文件或目录 Azure Python 错误 - FileNotFoundError: [Errno 2] No such file or directory Azure Python error Python 错误:FileNotFoundError: [Errno 2] 没有那个文件或目录 - Python error: FileNotFoundError: [Errno 2] No such file or directory Python 错误 FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python error FileNotFoundError: [Errno 2] No such file or directory: Python 3-FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3 - FileNotFoundError: [Errno 2] No such file or directory python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError: [Errno 2] 没有这样的文件或目录: - Python FileNotFoundError: [Errno 2] No such file or directory: FileNotFoundError: [Errno 2] 没有这样的文件或目录 [Python] - FileNotFoundError: [Errno 2] No such file or directory [Python]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM