9 Reading and Writing Files Flashcards

1
Q

How can you:
1. Create paths that work for your operating system

  1. Does it matter if you use / or \ in your code?
A
  1. Using the Path module:
    »>from pathlib import Path
    »>path(‘spam’, ‘eggs’,’what’)
    WindowsPath(‘spam/bacon/eggs’)
  2. It does, as Windows uses ! But using pathlib, which is newer than os.path, allows you to always use / in your code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can you:
1. Create a path-text string

  1. What is different in the text string and why is it necessary?
A
  1. Using str(path(message)):
    »>str(path(‘spam’, ‘eggs’,’what’)
    ‘spam\bacon\eggs’
  2. It creates 2 backslashes -because each backslash needs to be escaped by another backslash character
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What can Path & backslashes do?
  2. What does they evaluate to?:

> > > Path(‘spam’) / ‘bacon’ / ‘eggs’
Path(‘spam’) / Path(‘bacon/eggs’)
Path(‘spam’) / Path(‘bacon’, ‘eggs’)

A
  1. They work like ‘+’ in the string concatenation
  2. They evaluate to:
    WindowsPath(‘spam/bacon/eggs’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you find out your current working directory and change it?

A

> > > from pathlib import Path
import os
Path.cwd()
os.chdir(‘C://targetFolder’)

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

Create a folder named ‘waffles’,
inside a folder named ‘walnut’,
inside a folder named ‘delicious’,
in the C:/ folder

How can you create a single folder within ‘walnut’?

A

os.makedirs(‘C:\delicious\walnut\waffles’)

Path(r’C:\delicious\walnut\waffles\spam’).makedir()
-> No ‘s’ in the end!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What do ‘.’ and ‘..’ indicate?
  2. How can you return a string of the absolute path?
  3. How can you return a relative path from A to B? What happens if B is not given?
A
  1. ’.’ current folder
    ‘..’ parent folder
  2. os.path.abspath(path)
    »>os.path.abspath(‘.’)
  3. os.path.relpath(A, B)
    If B is not given, the current folder is used. So (A, ‘.’) == (A)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

C:\Users\Al\spam.txt

  1. Explain:
    Name, Parent, Drive, Stem, Suffix and Anchor
  2. How can you find each of them out?
A

C:\Users\Al\spam.txt

1.
Drive: Only on Win 'C:'
Anchor: root folder 'C:\'
Parent: folder that contains file 'Users\Al\'
Stem: spam
Suffix: .txt
Name: spam.txt
  1. Pass the path to a variable:
    p = Path(‘C:\Users\Al\spam.txt’)

Than: variable.*
* –> anchor, parent…etc., e.g.
»>p.anchor
‘C:\’

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

How can you find out the size that all files and folders within a certain folder have?

Root folder:
C:\Windows\System32

A
size = 0
for file in os.listdir('C:\\Windows\\System32'):
     size = size + os.path.getsize
     (os.path.join
     ('C:\\Windows\\System32', file)

The Join-Method combines folder and filenames - this part might be redundant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. How can you check whether a file or a folder path exist?

2. How would you check if a DVD is in your Computer?

A
1.
Assign path to variable p
>>>p.exists() 
>>>p.is_file()
returns True or False
  1. dDrive = Path(‘D:')
    dDrive.exists()
    »>False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Name 2x ways to open an existing file ‘hello.txt’ in your home folder.

A
  1. open(Path.home() / ‘hello.txt’)

2. open(‘C:\Users\Yassin\hello.txt’)

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

What is a File object? What methods can you use?

A

It is a value type, just like a list or a dictionary.

filename.*()
* –> open()
write()
read()
readlines() -> list value

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

How can you write to a text file, append additional information and than read its contents?

A
  1. Pass ‘w’ or ‘a’ as the second argument to open(). Pass it to a variable
    »>spam = open(filename, ‘w’)
  2. Pass write() to the variable
    »>spam.write(‘meassage’)
  3. Close it
    »>spam.close()
  4. Re-Open in append mode&raquo_space;>spam = open(filename, ‘a’)
  5. > > > spam.write(‘MESSAGE’)
  6. > > > spam.close()
  7. Re-Open in read mode
    »>spam = open(filename, ‘r’)
  8. Pass read() to a NEW variable
    »>content = spam.read()
    9.»>spam.close()
  9. Print text
    »>print(content)
    message
    MESSAGE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the benefit of the shelve module? 2x

What does it save?

What values does it contain?

A

txt.files are good for plain text, but to save data, e.g. from Python programmes, shelve is better.
Its open function works for both reading and writing

It creates 3 binary files.

It works like a dictionary, with keys and values.

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

How can you store a list in a shelve File ‘mydata’ and add it to the key ‘cats’?

A
>>>import shelve
>>>catsList = ['Soo', 'Poo', 'Joke']
>>>shelfFile = shelve.open('mydata')
>>>shelfFile[cats]='catsList'
>>>shelfFile.close()

> > > shelfFile = shelve.open(‘mydata’)
shelfFile[cats]
[‘Soo’, ‘Poo’, ‘Joke’]

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