I am trying a small project to read the the simple csv file by using bash file and python unforunately its shows
list index out of range error for argv[1]
main.py
import sys
import csv
def main(file_name):
fp = open(file_name)
reader = csv.reader(fp)
points = []
for row in reader:
x = row[0]
y = row[1]
print(y)
if __name__ == "__main__":
if len(sys.argv) > 0:
file_name = sys.argv[1]
main(file_name)
else:
print "<filename> must be the first argument"
#BAsh file
#!/usr/bin/env bash
chmod +x ./main.py
for f in ./sample_data/*.csv
do
printf "%-28s" $f
./main.py $f
done
This could happen if $f
contains "extra" characters that can be somehow interpreted by BASH, such as &
and ||
. Anyway, it's better to use ./main.py "$f"
so $f
was interpreted as a single argument.
Consider this example
f="echo hello"
echo $f
This produces: echo hello
but not hello
as $f
is interpreted as an argument.
Maybe your python file must start with #!/usr/bin/python
Also it throws error NameError: name 'reader' is not defined
because your for loop is not inside main function. After moving that for inside main function your code works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.