Python Forum

Full Version: 'file' object has no attribute 'writeline'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,  everybody.
I have a very large text file (1.3 terabytes). The first 8 lines of this file are meta-information. I want to remove these lines and then import the rest of the files into a NoSQL database.
but I'm getting the error 'file' object has no attribute 'writeline'.

I am using this Python code:

fout = open("subset.vc", "w")
with open( 'hugefile.vcf', "r" ) as fin:
   # Ignore header lines
   for _ in range(8):
       next(fin)

   # Transfer data to output file
   for line in fin:
       fout.writeline(line)

fout.close()
it's file.write(), not file.writeline()

Also, it's better like this

with open( 'hugefile.vcf', "r" ) as fin, open("subset.vc", "w") as fout:
    # Ignore header lines
    for _ in range(8):
        next(fin)
 
    # Transfer data to output file
    for line in fin:
        fout.write(line)
In the future, please post the entire traceback error between the error code tags.
(May-16-2017, 08:17 PM)sarah_k Wrote: [ -> ]Hi,  everybody.
I have a very large text file (1.3 terabytes). The first 8 lines of this file are meta-information. I want to remove these lines and then import the rest of the files into a NoSQL database.
but I'm getting the error 'file' object has no attribute 'writeline'.

I am using this Python code:

fout = open("subset.vc", "w")
with open( 'hugefile.vcf', "r" ) as fin:
   # Ignore header lines
   for _ in range(8):
       next(fin)

   # Transfer data to output file
   for line in fin:
       fout.writeline(line)

fout.close()

It would likely be faster with a sed or awk command:

https://unix.stackexchange.com/questions...e-with-awk
(May-16-2017, 08:27 PM)buran Wrote: [ -> ]it's file.write(), not file.writeline()

Also, it's better like this

with open( 'hugefile.vcf', "r" ) as fin, open("subset.vc", "w") as fout:
    # Ignore header lines
    for _ in range(8):
        next(fin)
 
    # Transfer data to output file
    for line in fin:
        fout.write(line)

Or print!
for _ in range(8):
  next(fin) # headers

for line in fin:
    print(line, file=fout)