python - Python 当前工作目录设置为主路径而不是脚本目录
问题描述
执行以下脚本时,为什么没有将 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 文件,右键单击输出C:/Python27/python.exe c:/Users/user/Desktop/test.py
命令的“在终端中执行 python 文件”。
如果我使用与项目解释器相同的 python 集从 pycharm 运行脚本,则会创建 json 文件,但为什么呢?
EDIT2:由于某种原因,当我执行脚本时,cwd 是我的主文件夹,不应该是 python 脚本的路径。我知道这可以解决,os.chdir(os.path.dirname(__file__))
但不应该这样对吗?
解决方案
确保您C:\Users\user\Desktop
在 VS Code 中打开您的文件夹,以便将 cwd 设置为您的桌面(如果您直接打开文件,那么它不会更改您的工作目录)。
推荐阅读
- r - 使用 FontAwesome 字形在 Rmarkdown 中渲染华夫饼图失败
- sql - 按连续范围分组的最小值和最大值
- security - 如何在 linux-kernel 4.18 上使用 register_security 注册安全模块?
- swift - Swift初始化程序的名称与类不同?
- ios - 如何使用 getDataInBackground 将整个 PFFile 数组转换为 UIImage 数组
- decentralized-identity - 我在哪里可以了解更多关于去中心化身份的信息?
- svg - 我如何正确获得 svg vmin?
- html - 为什么我的网页中会显示 p 标签?
- haskell - 如何使用 Haskell 中的理解从嵌套列表中删除空列表
- c - 改变动态 *char 的大小