简体   繁体   English

我如何在 python 的特定文件夹中保存.json 文件

[英]How do i save .json files in specific folder in python

I am using python and I wrote script, that makes from.csv a.json file... it works great but i would like to save those json files into different folder.我正在使用 python 并编写了脚本,该脚本来自.csv a.json 文件......它工作得很好,但我想将这些 Z4636DEEC76ECDF24D6 文件保存到不同的文件夹中。

Where do I write path?我在哪里写路径?

import csv
import json
import glob
import os


for filename in glob.glob("Csv/*.csv"):
csvfile = os.path.splitext(filename)[0]

jsonfile = csvfile + '.json'

with open(csvfile+'.csv') as f:
    reader = csv.DictReader(f)
    rows = list(reader)

with open(jsonfile, 'w') as f:
    json.dump(rows, f,indent=4)

You need to specify the full path of the json file.您需要指定 json 文件的完整路径。
Now you are just put the name of the file and the result of this is that the file is written in the same directory.现在您只需输入文件的名称,结果就是文件被写入同一目录中。

Try to do something like this;尝试做这样的事情;

with open("my/path/{}".format(jsonfile), 'w') as f:
    json.dump(rows, f,indent=4)

You might use os.path.join for this as follows, for example例如,您可以按如下方式使用os.path.join

jsondir = "jsons"
with open(os.path.join(jsondir,jsonfile), 'w') as f:
    json.dump(rows, f,indent=4)

disclaimer: I assume jsons catalog does already exists免责声明:我假设jsons目录确实已经存在

Here are a few useful hacks:以下是一些有用的技巧:

Using os and __file__ you can consitently (OS independent) get a file's directory使用os__file__您可以始终如一地(独立于操作系统)获取文件的目录

FILE_DIR: str = os.path.dirname(os.path.abspath(__file__))

Getting to parent folders can be done with pathlib可以使用pathlib访问父文件夹

from pathlib import Path

PARENT_DIR: str = Path(FILE_DIR).parent.as_posix()

Finally, you can get the user's home directory via os.path.expanduser("~") .最后,您可以通过os.path.expanduser("~")获取用户的主目录。

Once you have the folder you want, the rest should be straightforward.获得所需文件夹后,rest 应该很简单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何复制具有特定扩展名的文件夹目录中的文件,然后将其保存到新文件夹中而不覆盖已复制的文件? - How do I copy files in a folder directory with a specific extension and save it to a new folder with out overwriting copied files? Python,我如何在文件夹中找到以特定格式结尾的文件 - Python, how do i find files that end with a specific format in a folder 如何将一种特定类型的所有文件从一个文件夹复制到Python中的另一个文件夹 - How do I copy all files of one specific type from a folder to another folder in Python 如何在 python 中使用 selenium 从网站上抓取多个图像并将它们保存在特定文件夹中? - How do I scrape multiple images from a website and save them on a specific folder using selenium in python? 使用 python 将 json 文件保存到特定文件夹 - save json file to specific folder with python 我将文件(.jpg)保存在另一个文件夹中,我想在我的 python 程序中使用该文件。 我如何导入它们 - i save files(.jpg) in another folder and i want to use that files in my python program. how do i import them 如何在 JSON 中保存 python 代码 - How do I save python code in JSON 如何将具有特定文件扩展名的文件复制到我的python(2.5版)脚本中的文件夹中? - How do I copy files with specific file extension to a folder in my python (version 2.5) script? 如何使用 Python 在特定文件夹中找到今天创建的所有文件 - How do I find all the files that were created today in specific folder using Python 如何将文件上传到Django中的特定文件夹? - How do I upload files to a specific folder in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM