How to add lines to the beginning of a text file?

D

dean

Hello,

As the subject says how would I go about adding the lines to the beginning
of a text file? Thanks in advance.
 
S

Steven D'Aprano

Hello,

As the subject says how would I go about adding the lines to the
beginning of a text file? Thanks in advance.

I'm not aware of any operating system that allows you to insert data into
the middle or beginning of a file, although I've heard rumours that some
ancient operating systems (VAX?) may have.

To insert into a file, you have to do all the moving of data yourself. If
the file is small enough to fit into memory, the easiest way is something
like this:


# read the current contents of the file
f = open('filename')
text = f.read()
f.close()
# open the file again for writing
f = open('filename', 'w')
f.write("This is the new first line\n")
# write the original contents
f.write(text)
f.close()


But note that this isn't entirely safe, if your Python session crashes
after opening the file the second time and before closing it again, you
will lose data. For quick scripts, that's a minuscule risk, but for
bigger applications, you should use a safer method, something like this:


# read the current contents of the file
f = open('filename')
text = f.read()
f.close()
# open a different file for writing
f = open('filename~', 'w')
f.write("This is the new first line\n")
# write the original contents
f.write(text)
f.close()
# did everything work safely?
# if we get here, we're probably safe
os.rename('filename~', 'filename')


But note that even this is not entirely safe: it uses a predictable file
name for the temporary file, which is generally a bad thing to do, it
fails to flush data to disk, which means you're at the mercy of vagaries
of the file system, and it doesn't do *any* error handling at all.

The standard library really needs a good "save data to file" solution.
 
J

Jon Clements

Hello,

As the subject says how would I go about adding the lines to the beginning
of a text file? Thanks in advance.

I'd create a new file, then write your new lines, then iterate the
existing file and write those lines... If no errors occcur, issue a
delete for the old file, and a rename for new file to the old name.

hth

Jon.
 
D

Dennis Lee Bieber

I'm not aware of any operating system that allows you to insert data into
the middle or beginning of a file, although I've heard rumours that some
ancient operating systems (VAX?) may have.
Xerox CP/V IF (important point) the text file had been created using
the normal editor.

Reason:
CP/V supported three file organizations: "consecutive" -- the
equivalent of normal files on most all other OS's (that is, stream I/O);
"keyed" -- ISAM keyed headers; "random" -- the OS did /nothing/ with the
file except allocate a contiguous region of disk space (consecutive
files could jump around the disk; random files were pre-allocated and
all internal contents were up to the application).

The CP/V text editor used keyed file format, wherein the "line
number" was a simple ISAM key. Adding lines to the beginning of the file
merely meant doing keyed I/O with line numbers less than the first
number in the file. The editor normally increment in integers, but
supported three decimal places (I suspect it really incremented in 1000,
and scaled the result for display). Opening a consecutive file in the
editor results in conversion to keyed. One had to use a special command
to convert a keyed file to consecutive.

Side note: the Xerox FORTRAN IV random I/O used the "recno" as the
key <G>
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top