Skip to content

Chapter 5A: File management, input, output

Marta Vohnoutova edited this page Dec 20, 2018 · 10 revisions

Chapter 5A: File management, input, output

Input, output

->>> x = input("something:")
something:10
````
Input is always string, so use int(), but use /try - except ValueError/ to avoid errors.

```diff
->>> x = int(input("integer:"))
integer:10

About files

The term file management is the manipulation of data in a file or files and documents on a computer. The most basic tasks involved in file manipulation are reading data from files and writing or appending data to files.

Reading and writing files

The syntax for reading and writing files in Python is similar to programming languages like C, C++, Java, Perl, and others but a lot easier to handle. The syntax is:

file object = open(file_name [, access_mode][, buffering])

Parameters

file_name a string value that contains the name of the file that you want to access. For read only opening - do try - except FileNotFoundError
access_mode mode in which the file has to be opened, i.e., read, write, append, see the list of possible values. This is an optional parameter and the default file access mode is read (r).
buffering 0 - no buffering. 1 - line buffering is performed while accessing a file. Integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).

Modes

r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
rb+ Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

File attributes

file.closed Returns true if file is closed, false otherwise.
file.mode Returns access mode with which file was opened.
file.name Returns name of the file.
->>> f=open("attempt.txt","w")
->>> f.closed
False
->>> f.mode
'w'
->>> f.name
'attempt.txt'
->>> 

Reading files

Syntax is

fileObject.read([count])

->>> f = open("filename", "r")
->>> for line in f:
-         print(line.rstrip())
or
->>> f.read()     # if nothing in (), it reads all
or
->>> f.readline()
'This is the first line of the file.\n'
->>> f.readline()
'Second line of the file\n'
If you want to read all the lines of a file in a list you can also use 
->>> list(f) 
or 
->>> f.readlines()
->>> f.close()              # when finish - alwayas, especially when writing to a file, close it
“r” is optional, without “r”, the file is opened for reading only. The open() function returns a file object, which offers attributes and methods.

->>> f = open("LICENSE.txt","r")
->>> f.read(100)
'A. HISTORY OF THE SOFTWARE\n==========================\n\nPython was created in the early 1990s by Guid'
->>> f.read(1)
'o'
->>> 
File positioning
tell() current position within the file
seek(offset[, from]) changes the current file position. offset = number of bytes to be moved, /from / reference position from where the bytes are to be moved (0 = begin of file, 1 = current position, 2 = end of file.
->>> f.tell()
104
->>> f.seek(0,0)
0
->>> f.tell()
0
->>> f.read(100)
'A. HISTORY OF THE SOFTWARE\n==========================\n\nPython was created in the early 1990s by Guid'
->>> f.tell()
103

Write to files

Syntax is:

fileObject.write(string)

->>> f = open("attempt.txt","r+")
->>> f.write("I have a cat")
12
->>> f.seek(0,0)
0
->>> f.read()
'I have a cat'
->>> 

Renaming and deleting files and others commands about files

Import the module os

->>> import os
->>> os.getcwd()
'C:\\Users\\mvohnoutova\\AppData\\Local\\Programs\\Python\\Python35-32'
->>> 
Syntaxes
os.rename(current_file_name, new_file_name) rename the file
os.remove(file_name) remove the file
os.mkdir(“newdir”) create directory
os.chdir(“newdir”) change the current position to the directory
os.getcwd() displays the current directory
os.rmdir(‘dirname’) revoves directory

csv files

import csv

with open('employee_birthday.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')

Optional Python CSV reader Parameters

The reader object can handle different styles of CSV files by specifying additional parameters, some of which are shown below:

delimiter specifies the character used to separate each field. The default is the comma (‘,’).

quotechar specifies the character used to surround fields that contain the delimiter character. The default is a double quote (’ ” ‘).

escapechar specifies the character used to escape the delimiter character, in case quotes aren’t used. The default is no escape character.

Writing CSV Files With csv

You can also write to a CSV file using a writer object and the .write_row() method:

import csv

with open('employee_file.csv', mode='w') as employee_file:
    employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    employee_writer.writerow(['John Smith', 'Accounting', 'November'])
    employee_writer.writerow(['Erica Meyers', 'IT', 'March'])
The quotechar optional parameter tells the writer which character to use to quote fields when writing. Whether quoting is used or not, however, is determined by the quoting optional parameter:

If quoting is set to csv.QUOTE_MINIMAL, then .writerow() will quote fields only if they contain the delimiter or the quotechar. This is the default case. If quoting is set to csv.QUOTE_ALL, then .writerow() will quote all fields. If quoting is set to csv.QUOTE_NONNUMERIC, then .writerow() will quote all fields containing text data and convert all numeric fields to the float data type. If quoting is set to csv.QUOTE_NONE, then .writerow() will escape delimiters instead of quoting them. In this case, you also must provide a value for the escapechar optional parameter.