10. OS Flashcards

1
Q

os module

A

os.path.join(‘folder1’,’folder2’,’file.png’)

avoid different system using / or \

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

current working directory

A

os.getcwd()

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

change directory

A

os.chdir

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

relative file path

A

.\ same folder as in cwd

..\ parent folder

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

file path string

A

1) ‘C:\user\files’

2) r’C:\user\files’

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

converting path to raw string

A

myVariable = ‘This string is supposed to be raw '
print(r’%s’ %myVariable)

but technically no different on the string after parse

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

check exist

A

os.path.exists(‘path’)
isfile
isdir

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

break up a path

A

os. path.dirname()

os. path.basename()

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

whole director

A

os.listdir

use with isfile

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

create a folder

A

os.makedirs => create all of the folder

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

open txt file

A
myfile = open('abc.txt)
myfile = open('abc.txt, 'r')
myfile = open('abc.txt, 'w') -> it creates the file if not exist
myfile = open('abc.txt, 'a') -> it creates the file if not exist

mytext = myfile.read()
myfile.close()

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

shelve module

A

shelve file is a binary File, very similar to a dictionary.
shelve file has keys and values
can store list

shelfFile = shelve.open(‘mydata’)
shelfFile[‘Cats’] = [‘a’,’b’,’c’]
shelfFile[‘Dogs’] = [‘d’,’e’,’f’]
shelfFile.close()

print(list(shelfFile.keys()))

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

shutil module

A

shutil. copy (can rename while copy)
shutil. copytree
shutil. move (this is also used for rename)
shutil. rmtree (this will remove the folder and all content)

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

deleting file or path

A

os. unlink(‘abc.txt’) -> File

os. rmdir(‘my path’) -> remove folder that is empty

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

deleting file or path

A

os. unlink(‘abc.txt’) -> File
os. rmdir(‘my path’) -> remove folder that is empty

note remove permenantly

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

list of all file and subfolder

A

os.listdir()

17
Q

send2trash modeul

A

not basic module

18
Q

walkthrough a folder

A

for folderName, subfolders, filenames in os.walk(os.getcwd()):
print(folderName)
print(subfolders) -> list of string
print(filenames) -> list of string

19
Q

find all files

A
for entry in os.scandir(base_dir):
        if entry.is_file():
            yield entry.path
        elif entry.is_dir():
            yield from get_old_files(entry.path)