Hi,
I want to take input file after file. But for some reasons, it just take input of only one file not the other files. (sys.argv is not working)
A small example how I am using this code:
import sys
from sys import argv
with open(sys.argv[1], 'r') as f:
contents = f.read()
print (contents)
Even this code is not working.
The code is as follows:
#!/usr/bin/env
import collections
data = collections.defaultdict(list)
import sys
from sys import argv
file=open("../names.dmp","r")
#original=open("test.txt","r")
write=open("write.txt","w")
missing=open("missing.txt","w")
for line in file:
columns = line.strip().split("|")
data[columns[0].strip()].append(columns[1].replace("\t", ""))
tax=0
name=""
none=0
count=1
with open(sys.argv[1], 'r') as original:
for line in original:
line=original.readline()
if line.startswith(">"):
try:
chunk=line.rsplit('[', 1)[1]
index = chunk[:-2]
if len(index) <= 50:
for k,v in data.items():
if index in v:
tax=k
break
else:
continue
if tax != 0:
lne=original.readline()
for pine in original:
if pine.startswith("\n"):
break
else:
lne=lne+pine
finalline=line[:1] + tax + "|"+"gi_"+line[1:]+lne+"\n"
write.write(finalline)
tax=0
else:
lne=original.readline()
for pine in original:
if pine.startswith("\n"):
break
else:
lne=lne+pine
missing.write(line[:1] + str(none) + "|"+"gi_"+line[1:]+lne+"\n")
tax=0
except:
lne=original.readline()
for pine in original:
if pine.startswith("\n"):
break
else:
lne=lne+pine
missing.write(line[:1] + str(none) + "|"+"gi_"+line[1:]+lne+"\n")
tax=0
the code works fine when given input as a single file but it does not work if I use sys.argv.
Thanks!
Thanks for the comments!
I edited my script already based on the comments and now the script is working fine.
You can mark the comments as answers if they helped you out, so that other readers will see this thread as solved!
I moved our comments to answers, so they can get accepted.