我正在win10上设置报告路径,使用Path
方法创建文件路径
from os.path import dirname,abspath
from pathlib import Path
base_dir = dirname(abspath(__file__))
now = time.strftime("%Y-%m-%d-%H:%M:%S")
test_reports_address = Path(base_dir, 'report')
if not os.path.exists(test_reports_address):
os.mkdir(test_reports_address)
filepath = Path(test_reports_address, now + "_report.html")
fp = open(filepath, 'wb')
但它得到了例外:
Traceback (most recent call last):
File "C:Program FilesJetBrainsPyCharm Community Edition 2020.1.1pluginspython-cehelperspydevpydevd.py", line 1438, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:Program FilesJetBrainsPyCharm Community Edition 2020.1.1pluginspython-cehelperspydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "C:/workspace/pis/run.py", line 30, in <module>
fp = open(filepath, 'wb')
OSError: [Errno 22] Invalid argument: 'C:\workspace\pis\report\2020-07-20-14:45:08_report.html'
我想我在windows上使用了正确的\
来显示文件路径,还有什么我遗漏的吗?
Windows路径中不能包含某些字符。在这种情况下,您尝试使用禁止使用的:
。
The following reserved characters:
< (less than)
> (greater than)
: (colon)
" (double quote)
/ (forward slash)
(backslash)
| (vertical bar or pipe)
? (question mark)
* (asterisk)
https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file