Pathlib and Python Directory Management

Last Updated :
Discuss
Comments

Question 1

What is the main difference between PurePath and Path classes in the pathlib module?

  • PurePath is used on Windows only, while Path is used on UNIX systems only

  • PurePath handles I/O operations, while Path is used for path computation only

  • PurePath performs only path computations without I/O, while Path supports both computation and actual file operations

  • Both are exactly the same

Question 2

Which method is used to list all .py files inside a directory and its subdirectories?

  • Path.glob('*.py')

  • Path.listdir('*.py')

  • Path.rglob('*.py')

  • Path.walk('*.py')

Question 3

What does the Path.cwd() method return?

  • The path of the home directory

  • The path of the root directory

  • The path where Python is installed

  • The current working directory path

Question 4

What is the main difference between os.mkdir() and os.makedirs()?

  • os.mkdir() creates files, os.makedirs() creates directories

  • os.makedirs() can create nested directories, os.mkdir() cannot

  • os.mkdir() deletes directories, os.makedirs() renames them

  • Both functions do exactly the same thing

Question 5

Which method would you use to list all the files and folders inside the current working directory (non-recursively)?

  • os.walk()

  • os.listdir()

  • os.scan()

  • os.traverse()

Question 6

You want to delete a non-empty folder named old_data. What function should you use?

  • os.rmdir("old_data")

  • os.remove("old_data")

  • shutil.rmtree("old_data")

  • os.delete("old_data")

Tags:

There are 6 questions to complete.

Take a part in the ongoing discussion