Easy Python: The guide to learning how to program by Computer Easy

Easy Python: The guide to learning how to program by Computer Easy

Author:Computer, Easy [Computer, Easy]
Language: eng
Format: epub
Published: 2020-04-24T16:00:00+00:00


Managing files

To allow us to interact with the filesystem , Python provides us with the built-in open () function . This function can be invoked to open a file and return an object file . The latter allows us to perform various operations on the file, such as reading and writing. When we are done interacting with the file, we must finally remember to close it, using the file.close () method.

The open function

The open () function accepts several arguments but the two most important arguments are the name of the file we want to open, and the way it is opened.

The file name must be a string that represents a path that can identify the location of the file on the filesystem. The path can be relative to the current directory (for example 'file.txt' , 'subdir / file.txt' , '../file.txt' , etc.) or absolute (for example '/ home / Marco / file. txt ' ).

The mode is optional, and its default value is the string 'r' , i.e. read . This means that if we don't specify the way, Python will open the file for reading. If, on the other hand, we want to be able to write to the file, we can specify as a way the string 'w' , that is , write . When we open a file for writing, thus specifying the 'w' mode , two things can happen: if the file does not exist, it is created at the specified path; if it exists, the contents of the file are deleted.

>>> open ('test.txt') # the file doesn't exist, so Python gives error (FileNotFoundError)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

>>>

>>> open ('test.txt', 'w') # by opening it for writing, the file is created

<_io.TextIOWrapper name = 'test.txt' mode = 'w' encoding = 'UTF-8'>

>>>

>>> open ('test.txt', 'r') # now that it has been created, we can also open it for reading

<_io.TextIOWrapper name = 'test.txt' mode = 'r' encoding = 'UTF-8'>

>>>

>>> open ('test.txt') # if we omit the mode, the file is opened for reading ('r')

<_io.TextIOWrapper name = 'test.txt' mode = 'r' encoding = 'UTF-8'>

If we want to continue adding content to the end of a file, without deleting existing content, we can use the string 'a' ( append ) as a way. The strings 'r +' and 'w +' allow us to read and write simultaneously (like 'w' , also 'w +' deletes the contents of the file). The 'x' mode (exclusive creation) creates and opens a new file for writing, returning an error ( FileExistsError ) if the file already exists.

Finally, there are two ways of opening files: the textual mode (used for text files, such as .txt and .html files ) and the binary mode (used for binary files, such as jpg images or .mp3 audio ). These modes can be specified by adding to the mode string a t for text files (default, so it can be omitted) and a b for binary files.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.