1 - Opening and writing to an existing file

To write in existing a file, you must add one of the parameters to the open() function:

  1. "a" Append - will be added at the end of the file
  2. "w" Write - will overwrite any existing content
  3. "r+" Read and write without overwriting existing content

We will then say that the file is open in write mode. To write to in file open in write mode, we use the write() function. The syntax is:

file.write(content)

Example: open a file and add content to it:

# opening with preservation of existing content
f = open ("myFile.txt", "a")
f.write ("Here is content that will be added to the file without overwriting the content!")
f.close ()
# open and read the file after adding:
f = open ("myFile.txt", "r")
print (f.read ())

Example: open the file "myFile.txt" with overwriting of existing content:

# opening with overwriting of existing content
f = open ("myFile.txt", "w")
f.write ("Sorry! I deleted the content!")
f.close ()
# open and read the file after adding:
f = open ("myFile.txt", "r")
print(f.read())

2 Creating files in Python

To create a new file in Python, we use the open () method, with one of the following parameters:

  1. "x"   :   this opening mode, creates a file if it does not exist and returns an error if the file exists
  2. "a"   :   Append - will create a file if the specified file does not exist
  3. "w"  :   Write - will create a file if the specified file does not exist and if the file exists, it will be overwritten
  4. "r+" :   open in read and write mode. If the file does not exist, an error is returned.

 Example. Creation of a file named "myFile.txt":

f = open ("myFile.txt", "x") 
# returns an error if the file exists.

3 - Add lines to a file in Python with the writelines() method

The writelines() method allows you to add a list of strings or a list of lines to a file opened in write mode.

Example. add a list of lines to a file

f = open ("myFile.txt", "r +")
l = ["line1  n", "line2  n," line3  n "]
f.writelines (l)
print (f.read ())
f.close ()
"""
The output after execution:
line 1
line2
line3
"""




4 - Summary of Python methods associated with a file object with description:

  1. file.close (): close an open file.
  2. file.fileno (): returns an entire descriptor of a file.
  3. file.flush (): clears the internal buffer.
  4. file.isatty (): returns true if the file is connected to a tty device.
  5. file.next (): returns the next line of the file.
  6. file.read (n): reads the first n characters of the file.
  7. file.readline (): reads a single line in a string or file.
  8. file.readlines (): reads and returns the list of all the lines of the file.
  9. file.seek (): defines the current position of the file.
  10. file.seekable (): Checks whether the file supports random access. Returns true if yes.
  11. file.tell (): returns the current position in the file.
  12. file.truncate (n): truncates the size of the file. If n is supplied, the file is truncated to n bytes, otherwise truncated at the current location.
  13. file.write (str): writes the string str to the file.
  14. file.writelines (sequence): writes a sequence of lines or strings to the file
Younes Derfoufi
my-courses.net

Leave a Reply