0

I am trying to loop through all CSV files in a directory, do a find/replace, and save the results to the same file (same name). It seems like this should be easy, but I seem to be missing something here. Here is the code that I'm working with.

import glob
path = 'C:\\Users\\ryans\\OneDrive\\Desktop\\downloads\\Products\\*.csv'
for fname in glob.glob(path):
    print(str(fname))
    with open(str(fname), "w") as f:
        newText = f.read().replace('|', ',').replace(' ', '')
        f.write(newText)

I came across the link below, and tried the concepts listed there, but nothing has worked so far.

How to open a file for both reading and writing?

ASH
  • 20,759
  • 19
  • 87
  • 200
  • [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Nov 01 '18 at 17:45
  • Not posting as an answer as it doesn't use Python but if you have cygwin or bash for Windows you can achieve this with `sed` using the `-i` flag. – Jamie Scott Nov 01 '18 at 17:46
  • Can you try using "r+" instead of "w" when you open the file? – peachykeen Nov 01 '18 at 17:46
  • You need to see [this](https://stackoverflow.com/a/15976014/10362396) answer. It will tell you how to open file in r/w mode – tbhaxor Nov 01 '18 at 17:47
  • 1
    Possible duplicate of [How to open a file for both reading and writing?](https://stackoverflow.com/questions/6648493/how-to-open-a-file-for-both-reading-and-writing) – Maurice Meyer Nov 01 '18 at 17:47
  • 2
    @Prune I think you are being a little over-the-top here, it's clear from the code and description what they trying to do.. – Jamie Scott Nov 01 '18 at 17:48
  • Yeah ... figured it out ... by implementing the missing parts of the posting. Note the lack of closure or down votes ... – Prune Nov 01 '18 at 17:48
  • this has already been well answered https://stackoverflow.com/a/20593644/1951298 – Curcuma_ Nov 01 '18 at 17:49

2 Answers2

2

You need to open the file using 'r+' instead of 'w'. See below:

import glob
path = 'C:\\Users\\ryans\\OneDrive\\Desktop\\downloads\\Products\\*.csv'
for fname in glob.glob(path):
    print(str(fname))
    with open(str(fname), "r+") as f:
        newText = f.read().replace('|', ',').replace(' ', '')
        f.write(newText)
peachykeen
  • 4,143
  • 4
  • 30
  • 49
  • Ah, I just saw this. This is very, very close, but I want to overwrite what is in each file, not append the changes to the bottom of each file. How can I open each one, replace a few strings, and write only the changes to each file, without including the original contents of each file? Thanks. – ASH Nov 01 '18 at 18:32
  • 1
    Check this post out for that: https://stackoverflow.com/questions/2424000/read-and-overwrite-a-file-in-python – peachykeen Nov 01 '18 at 18:44
  • 1
    Yeap, that's it! Thanks so much!! – ASH Nov 01 '18 at 19:12
1

Here is the final (working) solution.

import glob
import fileinput
path = 'C:\\Users\\ryans\\OneDrive\\Desktop\\downloads\\Products\\*.csv'
for fname in glob.glob(path):
    #print(str(fname))
    with open(fname, 'r+') as f:
        text = f.read().replace(' ', '')
        f.seek(0)
        f.write(text)
        f.truncate()

Thanks for the tip, agaidis!!

ASH
  • 20,759
  • 19
  • 87
  • 200