0

I have a parent directory with a list of sub directories. Each of these sub directories contains a file named data.json. I want to walk through all the sub directories and carry out some editing operations on the data.json file. How do I achieve this ? presently I am doing the following :

  for dir,dirs, files in os.walk("."):
        for filename in files:
            if file == 'data.json':
             with open(file, 'r') as f:
             #carry out editing operation

But I dont see any editing happenning. I suspect the data.json file is not being located.

DAIRAV
  • 723
  • 1
  • 9
  • 31
Vasanth Raghavan
  • 162
  • 2
  • 12
  • 2
    You can't modify a file that's opened in read mode. You can read about the different modes [here](https://stackoverflow.com/a/23566951/4014959). Also, `file` just gives you the base file name, not the full path. I suggest you put a few `print` calls into your code so you can see what's happening. If you're using Python 3.4+ take a look at [`pathlib`](https://docs.python.org/3/library/pathlib.html#module-pathlib), it has a whole lot of great stuff for working with files and it's nicer than using `os.walk`. – PM 2Ring Jul 16 '18 at 10:00
  • I tried changing permission to 'w+', doesnt work. The problem is I dont see the code flow entering the if loop : if file == 'data.json': – Vasanth Raghavan Jul 16 '18 at 10:12
  • Of course 'w+' doesn't work. As the answer I linked previously says 'w+' overwrites the existing file if the file exists. You could use 'r+'. However, it's simpler and safer to open in 'r' mode, read the data, close the file (or let `with` close it automatically), and then open the file again in 'w' mode to save the modified data. That way you're much less likely to destroy your data if something goes wrong. – PM 2Ring Jul 16 '18 at 10:22

1 Answers1

2

First you need to correctly access your file with full path:

for path, subdirs, files in os.walk("."):
    for filename in files:
        if filename == 'data.json':
            fullpath = os.path.join(path, filename)

Then open it in desired mode, the one you included is read:

with open(fullpath, 'r') as f:
    ...

Read more about modes for open() here.

zipa
  • 27,316
  • 6
  • 40
  • 58