python - 带有 open() 错误 22(Windows 路径)
问题描述
我无法使以下代码正常工作:
path = "C:\\Users\\jiversen\\Documents\\Jsons\\"+jsonName+'.json'
with open(path,'w') as outfile:
json.dump(df,outfile)
我收到以下错误:
OSError Traceback (most recent call last)
<ipython-input-162-ad4856eeb7ee> in <module>()
7 #path = r "C:\Users\jiversen\Documents\CosmosDB Simulator\Jsons\"+jsonName+'.json'
8
----> 9 with open(abs_file_path,'w') as outfile:
10 json.dump(df,outfile)
OSError: [Errno 22] Invalid argument: 'C:\\Users\\jiversen\\Documents\\Jsons\\2018.04.06 12:00:00.000.json'
我尝试了以下方法来创建路径:
1
path = "C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
这会产生以下错误:
File "<ipython-input-166-405d2aae6e9c>", line 7
path = "C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
^
SyntaxError: EOL while scanning string literal
2
path = R"C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
这会产生以下错误:
File "<ipython-input-167-ff5d5da61135>", line 7
path = R"C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
^
SyntaxError: EOL while scanning string literal
3
import os
script_dir = os.path.abspath('C:\\Users\\jiversen\\Documents\\jsons\\') # i.e. /path/to/dir/foobar.py
rel_path = 'Jsons\\'+jsonName+'.json'
abs_file_path = os.path.join(script_dir, rel_path)
这会产生以下错误:
OSError Traceback (most recent call last)
<ipython-input-168-d286c1f58b6a> in <module>()
7 #path = R"C:\Users\jiversen\Documents\Jsons\"+jsonName+'.json'
8
----> 9 with open(path,'w') as outfile:
10 json.dump(df,outfile)
OSError: [Errno 22] Invalid argument: 'C:\\Users\\jiversen\\Documents\\Jsons\\2018.04.06 12:00:00.000.json'
4 没有子目录和空格
with open(jsonName+'.json','w') as outfile:
json.dump(df,outfile)
这会产生以下错误:
OSError Traceback (most recent call last)
<ipython-input-173-818eaaa66077> in <module>()
6 path = 'C:\\Users\\jiversen\\Documents\\Jsons\\'+jsonName+'.json'
7
----> 8 with open(jsonName+'.json','w') as outfile:
9 json.dump(df,outfile)
OSError: [Errno 22] Invalid argument: '2018.04.06-12:00:00.000.json'
附加信息
我已经复制粘贴了路径,并确保它一遍又一遍地存在 (C:\Users\jiversen\Documents\Jsons)
我在 jupyter-notebook 工作
对深空的回应
OSError Traceback (most recent call last)
<ipython-input-177-f09b6b3b06fe> in <module>()
10 # C:\Users\jiversen\Documents\Jsons\2018.04.06-12:00:00.000.json
11
---> 12 with open(full_path, 'w') as outfile:
13 json.dump(df, outfile)
OSError: [Errno 22] Invalid argument: 'C:\\Users\\jiversen\\Documents\\Jsons\\2018.04.06-12:00:00.000.json'
解决方案
Windows 不允许:
在文件名中使用冒号 ( )。
尝试
import os
jsonName = '2018.04.06-12.00.00.000'
# ^ ^ No colons!
path = r'C:\Users\jiversen\Documents\Jsons'
file_name = '{}.json'.format(jsonName)
full_path = os.path.join(path, file_name)
print(full_path)
# C:\Users\jiversen\Documents\Jsons\2018.04.06-12.00.00.000.json
with open(full_path, 'w') as outfile:
json.dump(df, outfile)
推荐阅读
- php - 如何在 Laravel 中提供缩放图像?
- python - Python zfill 在正则表达式中减一
- symfony4 - SF4:在可重用包中添加路由
- mysql - node.js 的 mariadb ER_ACCESS_DENIED_ERROR 用户访问被拒绝
- sql-server - 截断表时避免阻塞其他查询
- flask - AttributeError:“字节”对象没有属性“编码”
- c# - 更改 Microsoft SourceCode Console.WriteLine
- android - AlertDialog 阻止 Espresso 测试执行
- python - 我认为 jupyter-qtconsole 坏了,因为我运行的是 os x 10.10.5。不更新怎么解决?
- asp.net-core - 从剃刀页面处理程序返回部分视图