Reputation: 3173
i have a script that first reads in a table data with pandas, then does some computations:
import numpy as np
import sys
import pandas as pd
originaldata = pd.read_csv('../file.txt', sep='\t', skiprows = 31)
originaldata = originaldata.fillna(0)
.... (rest of the code)
I would like to pass the file name in command line rather than typing it in the code each time there is a different file. is my code right?
import numpy as np
import sys
import pandas as pd
filename = sys.argv[-1]
originaldata = pd.read_csv('filename', sep='\t', skiprows = 31)
originaldata = originaldata.fillna(0)
.... (rest of the code)
so on the command line i would type:
$python program1.py file.txt
update:
so from the comments i learned that one mistake is that the
originaldata = pd.read_csv('filename', sep='\t', skiprows = 31)
should be
originaldata = pd.read_csv(filename, sep='\t', skiprows = 31)
and instead of using sys.argv[-1] , argparse is more useful. so here i changed my code to:
import argparse
parser = argparse.ArgumentParser(description='program1 ')
parser.add_argument('-i','--input', help='Input file name',required=True)
originaldata = pd.read_csv(args.input , sep='\t', skiprows = 31)
note i'm using Anaconda 's Spyder IDLE which contains the pandas module, for my program, it runs well within the IDLE but is having problem with command line. Turns out Anaconda uses pythonwin , so after setting path to pythonwin, on my command line i typed :
$pythonwin program1.py file.txt
then a 'python for Win32' popped up and says 'still can't get my hands on win32ui' . i'm so confused now. does it have anything to do with me using a 64 bit computer?
Upvotes: 3
Views: 9028
Reputation: 11915
This line has a problem:
originaldata = pd.read_csv('filename', sep='\t', skiprows = 31)
It should be:
originaldata = pd.read_csv(filename, sep='\t', skiprows = 31)
This line will generally work, but....
filename = sys.argv[-1]
I wouldn't do it this way. You're just getting the final argument. Why not be more precise? I'd either use argparse
, which may be overkill in this case, but I'd probably just use sys.argv[1]
and possibly warn the user if they use an incorrect number of parameters by testing the len
gth of sys.argv
To solve the error, try adding:
import pandas as pd
You may need to pip install pandas
using the version of python you are running the script with.
Upvotes: 4