1

I am reading from a text file and writing to a CSV using DictWriter. Now I want to implement the same code for several text files and write to different CSV files. I want to use a properties.py file for this purpose like:

input1 = "file1.txt"  
output1 = "mycsv1.csv" 

input2 = "file2.txt"  
output2 = "mycsv2.csv"

and so on.

I tried using import and other methods specified in links such as "what would be a quick way to read a property file in python?" and "Using ConfigParser to read a file without section name", but could not solve this issue.

Part of my code:

with open("mycsv1.csv") as f:
    writer = csv.DictWriter(f, ['name', 'age', 'addr'], delimiter=',')
    writer.writeheader()
    with open("file1.txt") as fil:
    # rest of the operations

How can I change my code to make use of my properties.py file, i.e. each of the input files in the properties file are read one by one, and the output gets stored in the corresponding output csv?

user12
  • 13
  • 3
  • Please provide input and desired output. – Rohit-Pandey Sep 25 '19 at 17:41
  • Do you know the names of the files or you will read them from a directory? I mean, like this: https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory – powerPixie Sep 25 '19 at 18:26
  • You say you "want to use a `properties.py` file for this purpose". Why? There are other file types you could use (like `.json`, `.ini` or `.xml`) which would make this task a lot easier - why do you want to use a `.py`? – Grismar Sep 26 '19 at 03:12
  • @powerPixie from a directory – user12 Sep 26 '19 at 14:24

1 Answers1

0

I suggest using the *args in function definitions to help in this. In essence:

def main(*args):
    for input in args:
        # Your code to run per file
        with open(input) as f:
            # etc etc etc

Reference: https://www.geeksforgeeks.org/args-kwargs-python/

To use the above method, you now call it as:

main('file1.txt', 'file2.txt', ... , 'fileX.txt')

Things do change a bit if you are running properties.py as python properties.py. If so, then you can take advantage of either sys.argv (my personal preference) or argparse in your main function (reference: How to read/process command line arguments?).

The above would allow you to do this:

python properties.py input1.txt input2.txt ... inputX.txt
jayg_code
  • 571
  • 8
  • 21
  • Instead of typing the names of the files in command line, can I store the names in some file and then use that instead in the command line? – user12 Sep 26 '19 at 14:38
  • @user12, definitely. You can also take that approach so the params for `python properties.py` would now be a file name (i.e., `python properties.py fileslist.txt`. Of course that would mean reading that input file with the list before the `for` loop. – jayg_code Sep 27 '19 at 02:22