您现在的位置是:首页 > 数码 > 

OSError: [Errno 22] Invalid argument错误解决方案

2025-07-27 02:24:10
OSError: [Errno 22] Invalid argument错误解决方案 在做文件读取写入操作的时候遇见OSError: [Errno 22] Invalid argument: F:\\pythonProject\\Api\\common\\2022-0-11_15:7:2test.txt 的报错,单独的打印出文件路径明明是正确,

OSError: [Errno 22] Invalid argument错误解决方案

在做文件读取写入操作的时候遇见OSError: [Errno 22] Invalid argument: F:\\pythonProject\\Api\\common\\2022-0-11_15:7: 的报错,单独的打印出文件路径明明是正确,但使用python open()函数读写文件时就会报该错误

错误代码:

import os
import time
now = time.strftime(%Y-%m-%d_%H:%M:%S, time.localtime())
file_path = os.path.realpath(__file__)
file_name = os.path.join(os.path.split(file_path)[0], now		)
with open(file_name, 	w	) as file:print(file)

报错内容如下:

要特别注意,以当前时间命名文件时,时间格式化:%Y-%m-%d_%H:%M:%S 修改为%Y-%m-%d_%H_%M_%S 就能避免这种错误,注意一定格式化一定不要用冒号:

修改后的代码:

import os
import time
now = time.strftime(%Y-%m-%d_%H_%M_%S, time.localtime()) #一定要注意这里的时间格式,不要用冒号
file_path = os.path.realpath(__file__)
file_name = os.path.join(os.path.split(file_path)[0], now		)
with open(file_name, 	w	) as file:print(file)

执行结果如下:

 

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/shuma/857183.html

相关标签:无
上传时间: 2024-02-10 09:06:33
留言与评论(共有 18 条评论)
本站网友 今天是农历几月几日
10分钟前 发表
time.localtime()) file_path = os.path.realpath(__file__) file_name = os.path.join(os.path.split(file_path)[0]
本站网友 老鸡鸡
15分钟前 发表
print(file) 执行结果如下:  
本站网友 青岛房屋中介
4分钟前 发表
%M
本站网友 骚30
13分钟前 发表
%M
本站网友 北京抑郁症医院
6分钟前 发表
print(file) 执行结果如下:  
本站网友 四川三线建设
27分钟前 发表
time.localtime()) #一定要注意这里的时间格式,不要用冒号 file_path = os.path.realpath(__file__) file_name = os.path.join(os.path.split(file_path)[0]
本站网友 股鑫网123
17分钟前 发表
print(file) 报错内容如下: 要特别注意,以当前时间命名文件时,时间格式化:%Y-%m-%d_%H
本站网友 青岛租房信息网
3分钟前 发表
%M
本站网友 模块化ups
27分钟前 发表
time.localtime()) file_path = os.path.realpath(__file__) file_name = os.path.join(os.path.split(file_path)[0]
本站网友 阿根廷现任总统
23分钟前 发表
%S
本站网友 早点包子培训
29分钟前 发表
w ) as file
本站网友 泌尿外科手术
14分钟前 发表
time.localtime()) file_path = os.path.realpath(__file__) file_name = os.path.join(os.path.split(file_path)[0]
本站网友 巴南二手房
19分钟前 发表
%M
本站网友 降压药分类
11分钟前 发表
now ) with open(file_name
本站网友 泰国机票
16分钟前 发表
now ) with open(file_name
本站网友 浙江房价
7分钟前 发表
w ) as file
本站网友 推特怎么注册
6分钟前 发表
time.localtime()) file_path = os.path.realpath(__file__) file_name = os.path.join(os.path.split(file_path)[0]