🌍 All Study GuidesπŸ“Š DashboardπŸ“° BlogπŸ’‘ About
Google IT Automation with Python Professional Certificate β€’ STUDY MODE

MANAGING FILES & DIRECTORIES

QUESTION 1 OF 11

The create_python_script function creates a new python script in the current working directory, adds the line of comments to it declared by the 'comments' variable, and returns the size of the new file. Fill in the gaps to create a script called "program.py". (Image S1Q1 ) Answer: def create_python_script(filename): comments = "# Start of a new Python program" with open(filename, 'w') as file: file.write(comments) filesize = file.tell() return filesize print(create_python_script("program.py")) Great work! Your new python script is now ready for some real code!

QUESTION 2 OF 11

The new_directory function creates a new directory inside the current working directory, then creates a new empty file inside the new directory, and returns the list of files in that directory. Fill in the gaps to create a file "script.py" in the directory "PythonPrograms". ( Image S1Q2 ) Answer: import os def new_directory(directory, filename): # Before creating a new directory, check to see if it already exists if os.path.isdir(directory) == False: os.mkdir(directory) # Create the new file inside of the new directory os.chdir(directory) with open (filename, 'w') as file: pass # Return the list of files in the new directory return os.listdir() print(new_directory("PythonPrograms", "script.py")) Well done, you! Working with files and directories can be a little tricky, and you're getting the hang of it!

QUESTION 3 OF 11

Which of the following methods from the os module will create a new directory? Right on! os.mkdir() will create a new directory with the name provided as a string parameter.

A
path.isdir()
B
listdir()
C
mkdir()Correct Answer
D
chdir()
QUESTION 4 OF 11

The file_date function creates a new file in the current working directory, checks the date that the file was modified, and returns just the date portion of the timestamp in the format of yyyy-mm-dd. Fill in the gaps to create a file called "newfile.txt" and check the date that it was modified. ( Image S1Q4 ) Answer: import os import datetime def file_date(filename): # Create the file in the current directory with open(filename, 'w') as file: pass # Get the timestamp of when the file was last modified timestamp = os.path.getmtime(filename) # Convert the timestamp into a readable format, then into a string date_modified = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d') # Return just the date portion # Hint: how many characters are in β€œyyyy-mm-dd”? return ("{}".format(date_modified)) print(file_date("newfile.txt")) # Should be today's date in the format of yyyy-mm-dd Way to go! You remembered the commands to convert timestamp and format strings, to get the results that were requested.

QUESTION 5 OF 11

The parent_directory function returns the name of the directory that's located just above the current working directory. Remember that '..' is a relative path alias that means "go up to the parent directory". Fill in the gaps to complete this function. ( Image S1Q5 ) Answer: import os def parent_directory(): # Create a relative path to the parent # of the current working directory relative_parent = os.path.join(os.getcwd(), '..') # Return the absolute path of the parent directory return os.path.abspath(relative_parent) print(parent_directory()) Excellent! You made all the right moves to print the path of the parent directory!

QUESTION 6 OF 11

What is the difference between the readline() and read() methods? Right on! Both methods read from the current position. The readline() method reads one line, while read() reads until the end of the file.

A
The readline() method starts from the current position, while the read() method reads the whole file.
B
The read() method reads a single line, the readline() method reads the whole file.
C
The readline() method reads the first line of the file, the read() method reads the whole file.
D
The readline() method reads a single line from the current position, the read() method reads from the current position until the end of the file.Correct Answer
QUESTION 7 OF 11

Can you identify which code snippet will correctly open a file and print lines one by one without whitespace? for line in text: print(line) for line in text: print(text) print(line) for line in text: print(line.strip()) (CORRECT) Good work! Here, we are iterating line by line, and the strip() command is used to remove extra whitespace.

A
with open("hello_world.txt") as text:
B
with open("hello_world.txt") as text:
C
with open("hello_world.txt") as text:
D
with open("hello_world.txt") as text:
QUESTION 8 OF 11

What happens to the previous contents of a file when we open it using "w" ("write" mode)? You nailed it! When using write mode, the old contents get deleted as soon as the file is opened.

A
The new contents get added after the old contents.
B
A new file is created and the old contents are kept in a copy.
C
The old contents get deleted as soon as we open the file.Correct Answer
D
The old contents get deleted after we close the file.
QUESTION 9 OF 11

How can we check if a file exists inside a Python script? You got it! The os.path.exists function will return True if the file exists, False if it doesn't.

A
Renaming the file with os.rename.
B
Creating the file with os.create.
C
Using the os.path.exists function.Correct Answer
D
Deleting the file with os.remove.
QUESTION 10 OF 11

Some more functions of the os.path module include getsize() and isfile() which get information on the file size and determine if a file exists, respectively. In the following code snippet, what do you think will print if the file does not exist? import os file= "file.dat" if os.path.isfile(file): print(os.path.isfile(file)) print(os.path.getsize(file)) else: print(os.path.isfile(file)) print("File not found") 1024 2048 512 File not Found (CORRECT) Awesome! Because the file does not exist, getsize() will never be called and our error message will be printed instead.

A
file.dat
B
False
C
True
D
False
QUESTION 11 OF 11

What's the purpose of the os.path.join function? Right on! By using os.path.join we can concatenate directories in a way that can be used with other os.path() functions.

A
It creates a string containing cross-platform concatenated directories.Correct Answer
B
It creates new directories.
C
It lists the file contents of a directory.
D
It returns the current directory.

Ready to test your recall?

The create_python_script function creates a new python script in the current working directory, adds the line of comments to it declared by the 'comments' variable, and returns the size of the new file. Fill in the gaps to create a script called "program.py". (Image S1Q1 ) Answer: def create_python_script(filename): comments = "# Start of a new Python program" with open(filename, 'w') as file: file.write(comments) filesize = file.tell() return filesize print(create_python_script("program.py")) Great work! Your new python script is now ready for some real code!

How confident are you in this answer?