Files Flashcards
What is the syntax for opening a file?
with open('welcome.txt') as text_file: text_data = text_file.read() print(text_data)
Note that read() returns the file as one single string.
Also text_file is a file object that we then use to perform various actions.
How does the readlines() function work?
It reads the text file line by line.
with open('how_many_lines.txt') as lines_doc: print(lines_doc) for line in lines_doc.readlines(): print(line)
Note that readlines() returns a list with each line in the file.
How would we access only a single line in a file?
By calling the readline() method on the
file object.
with open('millay_sonnet.txt') as sonnet_doc: first_line = sonnet_doc.readline() second_line = sonnet_doc.readline() print(second_line)
What is the syntax for writing a file?
To write a file we use the open() function with a second argument ‘w’ for write mode.If the file already exists it will be over-written.
Note that the open() function uses ‘r’ by default as the second argument, so it is optional when reading a file.
Appending to an existing file?
If you do not want to overwrite the file you will need to use append mode.
what’s with ‘with’?
The ‘with’ keyword invokes a context manager for the file we’re calling open() on. The context manager takes care of opening the file when we call open() and closing the file when we leave the indent block. We can use the older method of opening and closing a file however this is not recommended since it will cause performance issues if it is not done correctly.
working with csv
To work with a csv file the best approach is to use the csv module.
CSVs with different delimiters?
Although csv means comma seperated values other delimeters are also valid.
Writing a csv file programmatically?
note that the order of the fileds does not need to match the source data.
Reading a JSON file?
The json module is used to process json files
note that the result of passing the file object to json.load() is a regular python dictionary and therefore we are able to perform regular dict. operations on the result.
Writing a JSON file?
note turn_to_json is the data we wish to convert to json which will be written in the file outout.json
What are the different modes available to open a file.
‘r’ - read the default mode
‘w’ - write mode will overwrite if the file already exist
‘a’ - append mode file alteady exist
‘a+’ - append, read and write mode.
‘x’ - make sure the file does not already exist.
Removing a file
import os if os.path.exists('my_file.txt'): os.remove('my_file.txt')
check if the file exists before carrying out an operation on the file.
How to remove a folder and all the files in it?
import os folder_name = 'sample_folder/' for file in os.listdir(folder_name): if os.path.exists(folder_name + file): os.remove(folder_name + file) os.rmdir('sample_folder')
What is a glob?
The ‘glob’ module is a built-in module used to find all the pathnames matching a spefic pattern according to the rules used by the Unix shell.
Note that the return type is a list.