我得到一个 "FileNotFoundError" 即使我正在*制作*一个文件
I'm getting a "FileNotFoundError" even though I'm *making* a file
我正在尝试创建一个包含文件的目录
# lines 1-5
import logging, os
unix_doc_loc = '~\byeStore'
win32_doc_loc = 'C:\byeStore'
[...]
# line 108
class Store():
[...]
# lines 132-148
def CacheCatalogue():
import sys, os, time, platform, json
byelogs.debug('sys, os and time were imported')
# catalogue = Store.GetCatalogue() # commented out as this isn't functional yet
catalogue = '{"lastUpdated": 1}'
catalogue = json.loads(catalogue)
byelogs.info('Catalogue recieved.')
byelogs.debug(catalogue)
if sys.platform == 'win32':
byelogs.info('Detected Windows ({}). Using "C:\byeStore\"...'.format(sys.platform)) # line 142 - the troublesome line
catcache = open('C:\byeStore\catalogue-cache.json', 'wt')
byelogs.debug('Opened catcache')
if catalogue['lastUpdated'] >= catcache['lastUpdated']:
byelogs.info('Catalouge in cache is old. Updating...')
catcache.write(catalogue)
byelogs.info('Updated.')
这是我的终端输出
Z:\ByeStore\ByeStoreLibs>featuretest.py
Traceback (most recent call last):
File "Z:\ByeStore\ByeStoreLibs\featuretest.py", line 5, in <module>
byestore.Store.CacheCatalogue()
File "Z:\ByeStore\ByeStoreLibs\src\byestore\__init__.py", line 142, in CacheCatalogue
catcache = open('C:\byeStore\catalogue-cache.json', 'wt')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\byeStore\catalogue-cache.json'
Z:\ByeStore\ByeStoreLibs>
我记得 open(file, 'wt')
中的 'wt'
部分生成了 个文件。我什至尝试使用 "x"
,但这也没有用。由于某些原因,我不能制作文件。
from pathlib import Path
d = "C:\byeStore\"
Path(d).mkdir(parents=True, exist_ok=True)
open(d + "catalogue-cache.json", 'w+')
这里有两件事:
- 先创建目录。
如果文件不存在,'w'
用于创建文件,而不是 't'
。
我正在尝试创建一个包含文件的目录
# lines 1-5
import logging, os
unix_doc_loc = '~\byeStore'
win32_doc_loc = 'C:\byeStore'
[...]
# line 108
class Store():
[...]
# lines 132-148
def CacheCatalogue():
import sys, os, time, platform, json
byelogs.debug('sys, os and time were imported')
# catalogue = Store.GetCatalogue() # commented out as this isn't functional yet
catalogue = '{"lastUpdated": 1}'
catalogue = json.loads(catalogue)
byelogs.info('Catalogue recieved.')
byelogs.debug(catalogue)
if sys.platform == 'win32':
byelogs.info('Detected Windows ({}). Using "C:\byeStore\"...'.format(sys.platform)) # line 142 - the troublesome line
catcache = open('C:\byeStore\catalogue-cache.json', 'wt')
byelogs.debug('Opened catcache')
if catalogue['lastUpdated'] >= catcache['lastUpdated']:
byelogs.info('Catalouge in cache is old. Updating...')
catcache.write(catalogue)
byelogs.info('Updated.')
这是我的终端输出
Z:\ByeStore\ByeStoreLibs>featuretest.py
Traceback (most recent call last):
File "Z:\ByeStore\ByeStoreLibs\featuretest.py", line 5, in <module>
byestore.Store.CacheCatalogue()
File "Z:\ByeStore\ByeStoreLibs\src\byestore\__init__.py", line 142, in CacheCatalogue
catcache = open('C:\byeStore\catalogue-cache.json', 'wt')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\byeStore\catalogue-cache.json'
Z:\ByeStore\ByeStoreLibs>
我记得 open(file, 'wt')
中的 'wt'
部分生成了 个文件。我什至尝试使用 "x"
,但这也没有用。由于某些原因,我不能制作文件。
from pathlib import Path
d = "C:\byeStore\"
Path(d).mkdir(parents=True, exist_ok=True)
open(d + "catalogue-cache.json", 'w+')
这里有两件事:
- 先创建目录。 如果文件不存在,
'w'
用于创建文件,而不是't'
。