Python Housekeeping and Maintenance Flashcards

1
Q

library module offers features
that help us deal with our local file
system

A

os

import os

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

os function that gets the current directory

A

os.getcwd()

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

os function that walks the elements of a directory tree.

A

os.walk()

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

os function that lists the directory of the specified path

A

os.listdir(path)

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

os function that returns the absolute path of the specified file name

A

os.path.abspath(name)

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

os function that returns true if the specified file or directory name exists

A

os.path.exists(name)

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

os function that returns true if the specified name is a directory.

A

os.path.isdir(name)

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

os function that returns true if the specified name is a file

A

os.path.isfile(name)

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

method to determine if file has a specific ending

A

.filename.endswith(‘txt’)

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

method to perform concatenation that accounts for path delimiter difference across operating platforms ( ‘/’ or ‘\’)

A

.join()

from os.path import join
thefile= os.path.join(dirname, filename)

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

os method to return the file size

A

.getsize()
os.path.getsize(thefile)

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

os method that deletes a file

A

.remove()

os.remove(thefile)

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

method to create a pipe that connects the output from one execution to the input of another execution

A

.popen()

cmd = ‘ls -l’
file_pointer = os.popen(cmd)
all_lines = file_pointer.read()
print(all_lines)

status = file_pointer.close()
print(status)

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