OS Library Flashcards

1
Q

Get the current working directory

A

os.getcwd()

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

Change the working directory

A

os.chdir(“C:\Users\Sarper\Desktop”)

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

List all entries in a directory

A

os.listdir(path=”C:\Users\Sarper\Downloads”)

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

Create a directory

A

os.chdir(“C:\Users\Sarper\Downloads”)
print(os.listdir())
os.mkdir(“New file”)
print(os.listdir())

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

Join path components

A

path = os.path.join(‘path1’, ‘path2’, “file.txt”)

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

Check if a path exists

A

os.path.exists(path)

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

Check if path is a file or a directory

A

os.path.isfile(“C:\Users\Sarper\Downloads\projects”)
os.path.isdir(“C:\Users\Sarper\Downloads\projects”)
os.path.isfile(“C:\Users\Sarper\Downloads\Picture1.png”)
os.path.isdir(“C:\Users\Sarper\Downloads\Picture1.png”)

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

Run system command

A

os.system(r”cd”)

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

Delete a file

A

[‘Picture1.png’, ‘Picture2.png’, ‘projects’]

import os

os.chdir(r”C:\Users\Sarper\Downloads”)

print(os.listdir())
os.remove(“Picture1.png”)
print(os.listdir())

# [‘Picture3.png’, ‘projects’]

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

Rename a file or a directory

A

os.rename(r”C:\Users\Sarper\Downloads\Picture2.png”, r”C:\Users\Sarper\Downloads\Picture3.png”)

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

Delete a directory

A

os.rmdir(path)

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