The first few lines of a python script I am writing look like this:
#!/usr/bin/env python
import os
import sys
import subprocess
import csv
import itertools
fin=open("filelist.txt",'r')
print(fin)
for line in fin:
TARG_FILE = open('fin', 'r')
spamreader = csv.reader(TARG_FILE, delimiter='\t')
for line in spamreader:
print(line[0:6])
Where filelist.txt is a text file in the same directory as the script. If I put fin in quotation marks, the function returns:
File "VCFParser.py", line 13, in <module>
TARG_FILE = open('fin', 'r')
IOError: [Errno 2] No such file or directory: 'fin'
but if I put in without quotation marks, I get:
Traceback (most recent call last):
File "VCFParser.py", line 11, in <module>
TARG_FILE = open(fin, 'r')
TypeError: coercing to Unicode: need string or buffer, file found
I suspect the problem is very similar to what is found here:
However, I am unable to get the script to work using the information there.
If I print(fin), it returns the appropriate filepath for reading:
print(fin)
16.31235000.31366000/vcfBeta-GS000012109-ASM.vcf.gz.summary.txt
What am I missing here?
Could it have anything to do with a newline at the end of every line in the filelist.txt file?