The subject of this lesson is how to receive or read data from an interactive user or files, as well as display or write to them; Accordingly, the text of the lesson will examine the file object and the print () and input () functions in Python. Some differences in the implementation of the new version of Python (3x branch) that are related to the subject of this lesson are also mentioned.
✔ Level: Introductory
Headlines
Lesson 10: File, input and output in Python
Files Standard file object Input () function Print () function
Files
Files are a logical resource of the computer, managed by the operating system, and are capable of storing information for a long time. Files are actually consecutive bits of zeros and ones that are stored on disk, usually in two separate formats:
- “Binary file” – such as: video files, image files, zip file, Python bytecode file and …
- “Text file” – such as: HTML file, Python module file and …
Are known.
As mentioned, files are nothing but a set of binary (or binary) values, and text files are actually a subset of binary files, except that the bits of a text file, depending on the type of encoding, correspond to a sequence of Characters are clear and printable. The content of this type of file is placed in separate rows and is readable by human using text editing programs. In a text file with ASCII (ASCII) encoding, every eight bits (one byte) is equal to one character – ski codes are seven bits, but on most computers there is one byte for each – for example, the word Python in the form of six bytes, which is Each is stored in the order of the binary value of the ASCII code of each of these characters. Ski only supports 128 characters – from code 0 to 127 (in base ten) – which is why standard Unicode encodings are used more today. Unicode supports a much larger set of characters than ski coding, which includes skiing; For example, in the UTF-8 coding of this standard, the first 128 codes (from codes 0 to 127) are equal to the same 128-character ski code. UTF-8 encoding presents Unicode characters in a variable length byte sequence (1 to 6 bytes); In this encoding, only one byte is considered for ski characters.
There is no meaning line in a binary file, and its bytes may display data that cannot be printed by any of the character encoders (UTF-8, ASCII, etc.) or may even be output if printed. To be incomprehensible to humans.
Python provides a type of object ready to access files in the program, which is created using the open (file, mode) function; the first argument is a string object containing the full file name (address + name + extension) of the item The comment is on a computer disk and the second argument is a string with a specific value and specifies the state in which this file should be opened; this argument is optional and its default value is ‘r’. They are equivalent to each other. Note:
>>> f = open('/home/saeid/Documents/sample.txt')
>>> f = open('/home/saeid/Documents/sample.txt', 'r')
>>> f = open('/home/saeid/Documents/sample.txt', mode='r')
The sample file path above is based on the Linux file system, which in Windows, for example, can be entered in one of two ways:
r'C:\Users\Saeid\Documents\sample.txt'
'C:\\Users\\Saeid\\Documents\\sample.txt'
The file argument can only be set with an empty file name without mentioning its path; In this case, the Python interpreter considers the current directory path for it. Using the getcwd () function of the os [Python Documents] module, we can get the current directory path of the program. [We will talk more about this module in the next lesson]:
>>> import os >>> os.getcwd() '/home/saeid'
The mode argument can also have one of the following values as needed:
- ‘r’: The file is opened in text format for reading only and the pointer is placed at the beginning. If a file with this name does not exist, a FileNotFoundError error (or exception) is reported. Equivalent to ‘rt’
- ‘w’: The file is opened in text format for writing only; The text inside it (if any) is deleted and the cursor is placed at the beginning. If a file with this name does not exist, it is created first and then opened. Equivalent to ‘wt’
- ‘a’: A file in text format is opened only to add text at the end of the text; The pointer is at the bottom of the file. If a file with this name does not exist, it is created first and then opened. Equivalent to ‘at’
- ‘x’: only in Python exclusive creation) – 3x) The file is created in text format and opens for writing; If this file already exists, a fileExistsError error (or exception) will be reported. Equivalent to ‘xt’
- ‘rb’, ‘wb’, ‘ab’ and ‘xb’: have the same explanation as the options above, but with the difference that they are used to work with binary files.
- ‘+ r’: The file is opened in text format for reading and writing and the pointer is placed at the beginning of it. If a file with this name does not exist, a FileNotFoundError error (or exception) is reported. Note that in this case, the act of writing from the beginning of the file will replace the new text with the current text. Equivalent to ‘+ rt’ or ‘r + t’
- ‘+ w’: The file is opened in text format for writing and reading; The text inside it (if any) is deleted and the cursor is placed at the beginning. If a file with this name does not exist, it is created first and then opened. Equivalent to ‘+ wt’ or ‘w + t’
- ‘+ a’: The file is opened in text format to add text at the end of the text as well as read; The pointer is at the bottom of the file. If a file with this name does not exist, it is created first and then opened. Equivalent to ‘+ at’ or ‘a + t’
- ‘+ x’: Only in Python 3x – the file is created in text format and opens for writing and reading; If this file already exists, a fileExistsError error (or exception) will be reported. Equivalent to ‘+ xt’ or ‘x + t’
- ‘+ rb’ or ‘r + b’ and ‘+ wb’ or ‘w + b’ and ‘+ ab’ or ‘a + b’ and ‘+ xb’ or ‘x + b’: the same explanation of the similar options above But with the difference that they are used to work with binary files.
- ‘rU’: Read a text file with Universal Newline support. Equivalent to ‘rtU’
- ‘rbU’: Read a binary with Universal Newline support.
In which:
- r: read
- w: write
- a: insert at the end (appending)
- t: Specify the text format for the file; The template is the default and does not need to be inserted
- b: Specify the binary format for the file
- +: The file opens for updating
- U: Universal Newline mode
In the contract of different operating systems, different strings – which can consist of one or more characters – are used to mark the end of the lines of a text file; For example, Windows uses CRLF (or ‘r \ n \’) and GNU-Linux uses LF (or ‘n \’). The Python file object uses the same host operating system contract by default to specify its Newline string; But if you want to open another operating system contract file in Python for reading, you have to do it in Universal Newline mode. In this case, when reading from a file, Python maps all the Newline strings in the file to the ‘n \’ character [PEP 278]; The os.linesep command returns the Newline string of the host operating system:
>>> import os >>> os.linesep # GNU/Linux '\n'
Opening a file to read from it is like input and opening a file to write about it is like output.
The open () function also has other optional arguments that you can refer to in the Python documentation.
File object methods
- write (string): Receives an object of type str or bytes and writes it inside the object of the file:
>>> text = '1) Python\n2) Programming\n3) Language\n'
>>> print(text)
1) Python
2) Programming
3) Language
>>>
>>> type(text)
<class 'str'>
>>> # Python 3x
>>> output = open('textfile.txt', 'w')
>>> output.write(text)
37
>>> output.close()
In Python 3x, the write method returns the number of characters it writes.
>>> # Python 3x >>> output = open('binaryfile.bin', 'wb') >>> output.write(text) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a bytes-like object is required, not 'str'
In Python 3x, because the type of binary strings (bytes) is separated from the regular strings (str), the str object cannot be used to write to a file opened in binary mode.
>>> # Python 3x >>> data = '1) Python\n2) Programming\n3) Language\n' >>> # Convert str to bytes >>> bdata = bytes(data, 'utf-8') >>> bdata b'1) Python\n2) Programming\n3) Language\n' >>> type(bdata) <class 'bytes'> >>> output = open('binaryfile.bin', 'wb') >>> output.write(bdata) 37 >>> output.close()
There is no difference between text and binary modes in GNU / Linux behavior; now if you open both textfile.txt and binaryfile.bin (in GNU / Linux) with a text editor, you will see that the contents of the two files are displayed similarly.
In Gnolinux, open the textfile.txt file with a text editor program:
1) Python 2) Programming 3) Language
In Gnolinux, open the binaryfile.bin file with a text editor program:
1 2 3 |
1) Python
2) Programming
3) Language
|
But in Windows when in text mode, if you open a file for reading, the ‘n \’ characters in it are returned as ‘r \ n \’, and if you open a file for writing, the string is returned each time. ‘r \ n \’ is written instead of ‘n \’. In binary mode these things are not done.
If we run the above commands in Windows; The two files with the low content will be displayed by the text editor program.
In Windows, open the textfile.txt file:
1 2 3 |
1) Python
2) Programming
3) Language
|
In Windows, open the binaryfile.bin file:
1 |
1) Python2) Programming3) Language
|
See also the example of the above code in Python 2x:
>>> # Python 2x
>>> output = open('textfile.txt', 'w')
>>> output.write('1) Python\n2) Programming\n3) Language\n')
>>> output.close()
>>> # Python 2x
>>> data = '1) Python\n2) Programming\n3) Language\n'
>>> type(data)
<type 'str'>
>>> output = open('binaryfile.bin', 'wb')
>>> output.write(data)
>>> output.close()
>>> # Python 2x
>>> bdata = b'1) Python\n2) Programming\n3) Language\n'
>>> type(bdata)
<type 'str'>
>>> output = open('binaryfile.bin', 'wb')
>>> output.write(bdata)
>>> output.close()
- () close: After finishing work with each file, you must close it; This method closes the opened file. The object of the file after calling this method, can not call any other method.
Python automatically closes the previous object when the reference count to one file object reaches zero or when the file variable points to another file object; But it is always better to close the file explicitly by the programmer.
You can also use the closed attribute to check if a file is open or closed; This attribute contains the value True if the file is closed:
>>> f.closed False >>> f.close() >>> f.closed True
- () read: reads all the contents of the file and returns in the form of a str type object – for text files in both Python and binary in Python 2x – or bytes – for binary files in Python 3x -:
>>> input = open('textfile.txt')
>>> content = input.read()
>>> input.close()
>>> type(content)
<class 'str'>
>>> content
'1) Python\n2) Programming\n3) Language\n'
>>> print(content)
1) Python
2) Programming
3) Language
>>>
>>> # Python 3x, Reading a binary file
>>> input = open('binaryfile.bin', 'rb')
>>> content = input.read()
>>> input.close()
>>> type(content)
<class 'bytes'>
>>> content
b'1) Python\n2) Programming\n3) Language\n'
>>> print(content)
b'1) Python\n2) Programming\n3) Language\n'
>>>
>>> # Python 2x, Reading a binary file
>>> input = open('binaryfile.bin', 'rb')
>>> content = input.read()
>>> input.close()
>>> type(content)
<type 'str'>
>>> content
'1) Python\n2) Programming\n3) Language\n'
>>> print content
1) Python
2) Programming
3) Language
>>>
This method also has an optional argument; This argument is an integer that specifies the number of characters (or bytes) to be read and returned from the file:
>>> f = open('textfile.txt') >>> f.read(5) '1) Py' >>> f.read(5) 'thon\n' >>> f.read(10) '2) Program' >>> f.read(4) 'ming' >>> f.read(1) '\n' >>> f.close()
Note the example code above; When the file is opened in this mode (rt), the pointer is at the beginning of the file, and with each reading of the file, the pointer position moves forward.
- readline (): Returns one line from the file to the Newline string on each call:
>>> f = open('textfile.txt') >>> f.readline() '1) Python\n' >>> f.readline() '2) Programming\n' >>> f.readline() '3) Language\n' >>> f.readline() '' >>> f.close()
- readlines (): Returns all rows of a file as a list object:
>>> f = open('textfile.txt') >>> cl = f.readlines() >>> cl ['1) Python\n', '2) Programming\n', '3) Language\n'] >>> cl[1] '2) Programming\n' >>> f.close()
- (writelines) list: Takes a list object whose members are all of type str as an argument and writes its members to the file in order:
>>> L = ['a', 'b', 'c', 'd\n', 'e'] >>> f = open('tf.txt', 'w') >>> f.writelines(L) >>> f.close()
The result of the above code; Open the tf.txt file with a text editor:
1 2 |
abcd
e
|
- seek (offset): The offset argument is an integer, and this method changes the pointer position of the file to that offset relative to the beginning of the file:
>>> f = open('textfile.txt') >>> f.seek(3) 3 >>> f.read(6) 'Python' >>> f.close()
- flush (): Saves buffer content in the file.
When you open a file for writing, everything you write in the buffer is placed in the buffer before it closes. Calling this method helps to transfer information from the buffer to the file without closing the file.
Command for
The open () function can also be used in the for loop; In this case, the lines of the file are scrolled each time:
>>> for line in open('textfile.txt'): ... print(line, end='') ... 1) Python 2) Programming 3) Language >>>
Since the text in our file contains Newline (here: ‘n \’), we changed the end argument of the print () function to prevent the insertion of ‘n \’ at the end of each line [in the print section we will talk about the end argument ].
Command with / as
Is a composite command that supports objects known as “Context Manager” in Python [PEP 343]. Some objects in Python – such as the file object – have a feature called “Context Manager”; To implement this feature, the object class must contain two special methods (__ enter__) and () __ exit__, which are called with / as at the “entry” and “exit” times, respectively.
The with / as command has a similar structure below:
with expression as variable: statement(s)
In this structure, the expression symbolizes the part of the command that returns an object of type Context Manager; This object is referenced to a variable using the as keyword. For example, the with / as structure of a file object is written below:
with open('textfile.txt', 'w') as output: output.write('text')
After creating the file object, this object must enter the execution of the with / as command; with / as does this by calling the __ () __ method. In fact, this method returns the same file object created, which is then referenced by as to the output variable. Also, with this structure, there is no need to call the close () method for the file object; Because this is done after the body commands are completed by calling the __ exit method () with / as; In fact, with / as ensures closing the file at the end of executing its body commands. Also in this structure, if an error (or exception) occurs when working with the file, the file will be closed first before reporting it.
Note that a file object must always be closed; When you want to use the for loop for a file object, it is better to use it within the with / as structure:
with open('textfile.txt') as f: for line in f: print(line)
The with / as command can also be used nested:
with A() as a: with B() as b: statements
Also, new versions of Python have been added to versions 2.7 and 3.1, which allow multiple Context Manager to be used at the same time:
with A() as a, B() as b: statements
Like the example of the code below that opens two files; Reads from one and writes some lines in another file:
with open('data') as fin, open('res', 'w') as fout: for line in fin: if 'key' in line: fout.write(line)
Standard file object
Three types of file objects are created by the Python interpreter, all three of which are accessible through the sys module:
- sys.stdin: standard input [Python documents]; This object is used to access all inputs in Python interactive mode – such as calling the input () function.
- sys.stdout: standard output [Python documents]; Used by print.
- sys.stderr: standard error [Python documents]; It is the object that receives the errors.
Not always, but the sys.stdin source can be considered a computer keyboard and the sys.stdout and sys.stderr source can also be considered a screen.
Input () function
This function is used in Python to get input from the user – in interactive mode – which has been modified in the new version with the old version.
Python 2x:
- () raw_input
- () input
In this branch of Python, two functions raw_input() (Python Documents) and input (Python Documents) are available for this purpose. The raw_input () function reads a line from the user and returns it as a str object:
>>> s = raw_input() Hello Python
By executing the first line command, the Python interpreter waits for the text to enter – in this example the text Hello Python is written – then by entering the Enter key, it references all the received characters in the form of a string object – str type – to the variable s:
>>> s 'Hello Python' >>> type(s) <type 'str'>
You can also use text to view the user as an argument in the function call:
>>> s = raw_input("What's your name? ")
What's your name? Alan
>>> s
'Alan'
>>> s = raw_input("How old are you? ")
How old are you? 41
>>> s
'41'
>>> type(s)
<type 'str'>
The input () function in Python 2x also has the same function as the command eval () (raw_input).
>>> x = 1 >>> y = eval('x + 1') >>> y 2 >>> type(y) <type 'int'>
See also the sample code below:
>>> eval("9 // 2")
4
>>> eval("9 /// 2")
File "<string>", line 1
9 /// 2
^
SyntaxError: invalid syntax
>>>
>>> a = '32'
>>> type(a)
<type 'str'>
>>> b = eval(a)
>>> b
32
>>> type(b)
<type 'int'>
>>> print eval("__import__('os').getcwd()")
/home/saeid
The __ import function ()’s [Python Documents] has the same function as the import command, except that it can be used dynamically during program execution to import various modules; In this case, the name of a module can even be used as a variable in the function argument.
Now that we know the function of the eval () function, let’s look at some examples of the input () function in 2x versions:
>>> s = input("What's your name? ") What's your name? Alan Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'Alen' is not defined >>>
In the example code above; User input – Alen text – did not match any syntax defined in Python and caused an error:
>>> s = input("What's your name? ")
What's your name? "Alan"
>>> s
'Alan'
>>> s = input("How old are you? ")
How old are you? 41
>>> s
41
>>> type(s)
<type 'int'>
And another example:
>>> a = raw_input() 3 * 4 ** 5 >>> a '3 * 4 ** 5' >>> b = input() 3 * 4 ** 5 >>> b 3072
Python 3x:
- () input
In this branch of Python, there is no input () function from the 2x branch (deleted) and only the raw_input () function remains, which has also been renamed to input () (Python documents).
The input () function in Python 3x is the same as the raw_input () function in Python 2x:
>>> s = input("What's your name? ") What's your name? Alan >>> s 'Alan' >>> type(s) <class 'str'>
Print () function
Python 2x:
In this branch of Python print is implemented in the form of a simple command in Python [Python documents]. This command first converts one or more objects to str and then outputs:
>>> s = 'Python'
>>> print s
Python
>>> print s, "2.x"
Python 2.x
>>> print 4 * 5 / 2, 3 * 3
10 9
>>> print
>>>
- Objects must be separated by a comma.
- By default, this command places a space letter (a space key) between objects.
- If a computational or logical expression is given to this command, the result is first calculated or evaluated and then converted to str.
- The print command creates a blank line in a single form.
The print command also puts an ‘n \’ at the end of each line by default; To cancel this behavior, you can put a comma at the end of the command:
>>> for a in range(5): ... print a ... 0 1 2 3 4 >>> >>> for a in range(5): ... print a, ... 0 1 2 3 4 >>>
This command also has the ability to write to a file object other than the standard object; For this purpose, the following pattern should be followed:
>>> text = 'Hi :)' >>> output = open('textfile.txt', 'w') >>> print >> output, text >>> output.close()
Python 3x: The print command is implemented as a print () function in 3x Python versions [Python Documents]; The pattern for this function is as follows:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- objects *: Indicates the objects we want to put in the output. Which can be anything – which is equal to the representation of a blank line – or it can be one or more objects – in which case the objects must be separated by a comma -:
>>> print()
>>>
>>> s = 'Python'
>>> print(s)
Python
>>> print(s, '3x')
Python 3x
>>> print(4 * 5 / 2, 3 * 3)
10.0 9
- sep: Specifies the string to be placed between objects. The value of this argument by default is a space letter (a space key). The value passed to this argument must be a string object or None:
>>> print(s, '3x', sep='-') Python-3x >>> print(s, '3x', sep=None) Python 3x
- end: Specifies the string to be placed at the end of each row. The value of this argument is ‘n \’ by default. The value passed to this argument must be a string object or None:
>>> for a in range(5): ... print(a) ... 0 1 2 3 4 >>> >>> for a in range(5): ... print(a, end=' ') ... 0 1 2 3 4 >>>
- file: Specifies the output that an object must have with the write (string) method in its class. This argument is set to standard output by default. This function cannot be used in binary mode of files:
>>> output = open('textfile.txt', 'w') >>> print('Hi :)', file=output) >>> output.close()
- flush: This argument has been added to the Python print () function since version 3.3. When the output is on a file; By setting this option to the True value, the text transfer to the file is done without wasting time.
This function is also available using the low import command in versions 2.6 and 2.7 of Python [Python Documents]:
from __future__ import print_function
😊 I hope it was useful