Append a record to the end of a text file

From Rosetta Code
Revision as of 10:26, 12 September 2011 by rosettacode>NevilleDNZ (Append torture test.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Append a record to the end of a text file is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Many systems offer the ability to open a file for writing, such that any data written will be appended to the end of the file. Further, the file operations will always adjust the position pointer to guarantee the end of the file, even in a multitasking environment.

This feature is most useful in the case of log files, where many jobs may be appending to the log file at the same time. Or where care must be taken to avoid not to concurrently overwrite the same record from another concurrent job.

Task: Given a two record sample for a mythical passwd file:

  • Write this record out in the typical system format.
    • Ideally these records will have named fields.
  • Close the file, then reopen the file for append.
    • Append the new record to the file and close the file again.
    • Take appropriate care to avoid concurrently overwrites from another job.
  • Open the file and demonstrate the new record has indeed written to the end.

Ensure the program appends a record to the end of a file in a multi-tasking environment.

Finally: Provided a summary of the languages capabilities in its own summary table.

Alternatively: If the language's appends facilities does not guarantee that writes always appends to the end of the file (especially where more then one job has the file open) then note this restriction. But if possible additional provide an example - possibly using locking etc... - of what can be achieved.

Python

Append Capabilities.
Data Representation IO
Library
Append
Possible
Automatic
Append
Multi-tasking
Guaranteed
In core On disk
dict CSV text file builtin
instance CSV text file builtin ? ? ?

From a "dict" to a CSV File

<lang python>#############################

  1. Create a passwd text file
  2. note that UID & gid are of type "text"

passwd_list=[

 dict(account='jsmith', password='x', UID="1001", GID="1000",
      GECOS=dict(fullname='Joe Smith', office='Room 1007', extension='(234)555-8917',
                 homephone='(234)555-0077', email='jsmith@rosettacode.org'),
                 directory='/home/jsmith', shell='/bin/sh'),
 dict(account='jdoe', password='x', UID="1002", GID="1000", 
      GECOS=dict(fullname='Jane Doe', office='Room 1004', extension='(234)555-8914',
                 homephone='(234)555-0044', email='jdoe@rosettacode.org'),
      directory='/home/jsmith', shell='/bin/sh')

]

passwd_fields="account password UID GID GECOS directory shell".split() GECOS_fields="fullname office extension homephone email".split()


def passwd_text_repr(passwd_rec):

 passwd_rec["GECOS"]=",".join([ passwd_rec["GECOS"][field] for field in GECOS_fields])
 return ":".join([ passwd_rec[field] for field in passwd_fields ])

passwd_text=open("passwd.txt","w") for passwd_rec in passwd_list:

 print >> passwd_text,passwd_text_repr(passwd_rec)

passwd_text.close()

del passwd_list, passwd_text

  1. Load text ready for appending

passwd_text=open("passwd.txt","a+") new_rec=dict(account='xyz', password='x', UID="1003", GID="1000",

            GECOS=dict(fullname='Z Yz', office='Room 1003', extension='(234)555-8913',
                       homephone='(234)555-0033', email='xyz@rosettacode.org'),
            directory='/home/xyz', shell='/bin/sh')

print >> passwd_text, passwd_text_repr(new_rec) passwd_text.close()

  1. Finally reopen and check record was appended

passwd_list=list(open("passwd.txt","r")) if "xyz" in passwd_list[-1]:

 print "Appended record:",passwd_list[-1][:-1]

</lang> Output:

Appended record: xyz:x:1003:1000:Z Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/sh