0

I have just started learning Python, and this question might sound too basic. I have a task to be done on a bunch of CSV files and I have written 4 scripts for the same.

Script1: Merges all the CSV files into one large file.
Script2: Deletes any empty rows in the large file.
Script3: Performs the needed operation on large file to give output CSV file
Script4: Deletes any empty rows in the output CSV file.

I was wondering if there is any way I can have all these 4 scripts tied together, under one name, so that when I run that, it will essentially run all these 4 scripts in a sequential order.

Any suggestions on this?

Thanks in advance!

dotpy_novice
  • 335
  • 2
  • 4
  • 16
  • See [this answer](http://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script). – initall May 20 '15 at 06:17
  • this [similar SO question](http://stackoverflow.com/questions/7974849/how-can-i-make-one-python-file-run-another) might help – Aakash Jain May 20 '15 at 06:19

3 Answers3

1

You can just combine all the actions into one script:

import csv
import os
import glob

rows = list()
path_to_files = r'C:/csv-directory'
file_list = ['file1.csv', 'file2.csv']

# Step 1 & 2 - read all files into one big collection
#              and ignore any blank lines

# If you have a large list of files:
# for filename in glob.iglob('{}/*.csv'.format(path_to_files)): 
#   with open(filename) as f:

for filename in file_list:
   with open(os.path.join(path_to_files, filename)) as f:
      reader = csv.reader(f, delimiter=',')
      rows += list(reader)

# Step 3 & 4 - Perform some operation on the
#              big CSV file, and provide output
#              as CSV file
with open(os.path.join(path_to_files, 'result.csv'), 'w') as f:
   writer = csv.writer(f, delimiter=',')
   for row in rows:
       # do something with row
       writer.writerow(row)
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • Thanks a lot! But I have over 40 CSV files. So I think adding all the names in the file_list will be a tedious task. Also, the names are pretty long. – dotpy_novice May 20 '15 at 06:24
0

Since apparently each script saves to disk, you can simply run:

python script1.py; python script2.py; python script3.py; python script4.py

From command line. If you prefer a python script to do that for you, just create a script (e.g. wrapper.py) as follows:

execfile('script1.py')
execfile('script2.py')
execfile('script3.py')
execfile('script4.py')

Then you can simply run python wrapper.py from command line.

jorgeh
  • 1,727
  • 20
  • 32
  • Yes I have been running each of the script individually until now. Just trying to put them all under one wrapper, so that it reduces the effort further! Thanks a lot! :) – dotpy_novice May 20 '15 at 06:20
0

Please try this,

os.system

You could call it using os.system("mergeFiles.py")

or

The subprocess module of Python

or

Python's execfile

aksappy
  • 3,400
  • 3
  • 23
  • 49