def convert(): #function to convert .yaml to .json
in_file = filepath #assigning input file from GUI to variable
out_file = savepath #assigning output path from GUI to variable
yaml = ruamel.yaml.YAML(typ='safe')
with open(in_file ) as i: #opening yaml file
data = yaml.load(i)
with open(out_file, 'w') as o: #writing json file
json.dump(data, o, indent=2)

- 370,779
- 53
- 539
- 685

- 29
- 1
- 10
1 Answers
Your code is not formatted properly. But let's say your code looks like:
def convert():
in_file = filepath #assigning input file from GUI to variable
out_file = savepath #assigning output path from GUI to variable
yaml = ruamel.yaml.YAML(typ='safe')
with open(in_file ) as i: #opening yaml file
data = yaml.load(i)
with open(out_file, 'w') as o: #writing json file
json.dump(data, o, indent=2)
You must change some logic here:
You must get file names information instead of file name. It can be a list of paths which can be got from
glob
. Then you can get individual file name looping in files.Since you have multiple files you cannot provide 1 file name and save all your files with same name. The best I can think of is (Let's say you are converting yaml files to json files) you can replace the extension of the file and save it with the same name.
Getting file names:
The
glob
module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order.
see glob
Now let's use it:
from glob import glob
files = glob("/your/path/*.yaml")
for file in files:
print(files)
Here file is path for each yaml file. Now you can open/use that file name inside the loop and do whatever you want.
Saving files
To save the file as json you can:
- Add .json to the file name
Basically use + or fstrings to add .json.
json_file_name = file + ".json"
or
json_file_name = f"{file}.json"
Be aware that your file names might look weird. Because you will have my_file.yaml
changes to my_file.yaml.json
.
- Replace yaml with json
Since file paths are strings you can use replace
method and replace the file name.
json_file_name = file.replace("yaml", "json")
Here your file name would look better but it will change all yaml
s in the name. So if your file name has yaml in it it will be changed as well.
Like my_yaml_file.yaml
changes to my_json_file.json
.

- 1,743
- 2
- 14
- 22
-
Here is my code if more clarification is needed---https://github.com/saurabhkingp/school_management/blob/main/test.py – Master Oct 05 '21 at 11:44
-
Ok, so I used filedialog.askdirectory() to get the path from GUI to a variable but when i am using that variable to pass inside glob like this-- files = glob("filepath/*.yaml") for file in files: print(files) it is giving me error D_Lib: debug printing for files [.*] and level [100] is turned on D_Lib: debug printing for files [.*] and level [200] is turned on D_Lib: debug printing for files [.*] and level [300] is turned on 20496:.../engine/vf_shex/vf_shex.cpp(91): INFO: DllCanUnloadNow returned S_OK. What to do now? – Master Oct 07 '21 at 16:55
-
Does this help? https://stackoverflow.com/a/43980056/2681662 – MSH Oct 07 '21 at 18:20
-
Yes that helped, I think it's just a warning – Master Oct 07 '21 at 19:05
-
Now when I am using def open_file(): global filepath filepath=filedialog.askdirectory(title="Select a Directory") files = glob("filepath/*.yaml") for file in files: print(files) <---it prints nothing and when I print files outside for loop then it prints an Empty List. It gives the desired output i.e. C/some/folder/some.yaml into list when I give the path manually i.e. files = glob("C/some/my/path/*.yaml"). Can you tell like how to do that. That'll be great help – Master Oct 07 '21 at 19:17
-
I want to take path from user using tkinter GUI filedialog method – Master Oct 07 '21 at 19:19
-
You're providing a string (literally) as a path to glob: `glob("filepath/*.yaml")`. Here the `"filepath"` is a string not the value of the variable `filepath`. It's not what you want. You have to join your filepath (wich is a variable) to `"*.yaml"`. Here how it had to be: `glob(filepath + "/*.yaml")`or a better version: `glob(os.path.join(filepath, "*.yaml"))`. With the second approach you will have no issue with different operating systems. However you must import the `os` first. – MSH Oct 08 '21 at 07:32
-
Thanks, it worked perfectly the way I wanted to. – Master Oct 10 '21 at 14:04