Files Flashcards

1
Q

What is the syntax for opening a file?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the readlines() function work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would we access only a single line in a file?

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntax for writing a file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Appending to an existing file?

A

If you do not want to overwrite the file you will need to use append mode.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what’s with ‘with’?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

working with csv

A

To work with a csv file the best approach is to use the csv module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

CSVs with different delimiters?

A

Although csv means comma seperated values other delimeters are also valid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Writing a csv file programmatically?

A

note that the order of the fileds does not need to match the source data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Reading a JSON file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Writing a JSON file?

A

note turn_to_json is the data we wish to convert to json which will be written in the file outout.json

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the different modes available to open a file.

A

‘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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Removing a file

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to remove a folder and all the files in it?

A
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')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a glob?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is pickling?

A

Pickling is the process used by python to serialize and deserialize objects. The pickle module is used to achieve this.

Note that pickling is not secure and should only be used with trusted sources!

17
Q

Serializing objects with the pickle module?

A
18
Q

Deserializing objects with the pickle module?

A