Python Forum
'file' object has no attribute 'writeline'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'file' object has no attribute 'writeline'
#1
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()
Reply
#2
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)
Reply
#3
In the future, please post the entire traceback error between the error code tags.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
(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
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#5
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,674 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,868 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 730 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,334 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,594 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,902 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  'dict_items' object has no attribute 'sort' Calli 6 4,475 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,107 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,356 Jun-24-2022, 08:37 AM
Last Post: Anushka00
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,875 Apr-27-2022, 09:27 AM
Last Post: Anldra12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020