0

I have bunch of .status files which content I want to change. I wrote a simple script:

import fileinput
for line in fileinput.FileInput("Bremgarten_AV.status",inplace=1):
    line = line.replace("processing","upload")
    print line

I would like define a task that is valid not only for file Bremgarten_AV.status but all .status files. Do you know guys how to it?

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274

2 Answers2

1
import os
import fileinput

for _, _, files in os.walk("."):      # _, _, stands for not used elements (in triples)
    for name in files:
        if name.endswith((".status")):
            for line in fileinput.FileInput(name, inplace=1):
                line = line.replace("processing","upload")
                print line
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • Note don't have to assign all elements of a tuple when you don't use them afterwards. A common practice is to put them in `_` so that the reader understands they won't be used later on (see http://stackoverflow.com/a/5893946/4653485). Nothing mandatory, just a habit. – Jérôme Nov 04 '16 at 09:18
  • Please consider to accept my answer (click on the check mark) if it was useful for you. Thanks. – MarianD Jul 05 '17 at 22:45
0

You can also try this to avoid looping on unwanted files (i.e. that do not end with '.status'):

import os
import fileinput

# list of all your files in your path
list_files = [f for f in os.listdir(path) if f.endswith(".status")]

for f in list_files:
    for line in fileinput.FileInput(f, inplace=1):
        line = line.replace("processing","upload")
MMF
  • 5,750
  • 3
  • 16
  • 20