为什么在执行以下脚本时无法在当前工作目录(脚本位置)中将json文件写入磁盘? dump()不应该这样做吗?
def getShotFolders():
shotDict = {
"root" : shotExample,
"shot_subdirs" : []
}
for root, dirs, files in os.walk(shotExample, topdown=False):
if root != shotExample:
shotDict["shot_subdirs"].append(os.path.relpath(root,shotExample))
pprint(shotDict)
with open("shot_folderStructure.json", "w") as write_file:
json.dump(shotDict, write_file )
getShotFolders()
编辑:好的,我从vscode运行我的python文件,并右键单击“在终端中执行python文件”,这会输出C:/Python27/python.exe c:/Users/user/Desktop/test.py
命令。
如果我使用与项目解释器相同的python集从pycharm运行脚本,则会创建json文件,但是为什么?
EDIT2:好,由于某些原因,当我执行脚本时,cwd是我的主文件夹,不应该到python脚本的路径。我知道os.chdir(os.path.dirname(__file__))
可以解决这个问题,但是不应该这样吗?
答案 0 :(得分:0)
请确保要在VS Code中打开C:\Users\user\Desktop
文件夹,以便将cwd设置为桌面(如果直接打开文件,则不会更改工作目录)。