10 Organizing Files Flashcards

1
Q
  1. What does shutill stand for?
  2. What is its structure?
  3. What data type can shutil use?
A
  1. shell utilities
  2. shutil.copy(source, destination)
    shutil.copytree(… , …)
    shutil.move(
    shutil.remtree(path)
    –> after destination it is optional to use
    destination\newName.filetype
  3. Both source and destination can be either strings or Path objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can copy you the file spam.txt to another folder:

  1. keeping its name
  2. changing the name to spam2.txt
  3. copy the whole folder (incl. subfolders & files)

Use the home path as your folder path

A

p = Path.home()

  1. shutil.copy(p/spam.txt, new/path)
  2. shutil.copy(p/spam.txt, new/path/spam2.txt)
  3. shutil.copytree((p/spam, new/path/spamBackup)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. How can you move:
    C:\bacon.txt to C:\eggs?
  2. What happens, if the folder eggs exists in the C:\ directory?
  3. Wat happens if the folder eggs does NOT exist?
  4. What happens if the file bacon.txt exists in the destination directory?
A
  1. shutil.move(‘C:\bacon.txt’, ‘C:\eggs’)
  2. Than bacon.txt is moved into C:\eggs
  3. Than bacon.txt will be renamed as eggs (without the .txt extension!)
  4. Than the existing file is overwritten
    • > Therefore, always be careful with move!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you:

  1. Delete a single file
  2. Delete a folder empty of files/subflders
  3. Delete a folder that is not empty?
A
  1. Delete a single file
    os. unlink(path)
  2. Delete a folder empty of files/subflders
    os. rmdir(path)
  3. Delete a folder that is not empty?
    shutil. rmtree(path)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Write a program to delete all .txt files in a folder
  2. How could you pre-check which files are about to be deleted? For example, if you accidentally typed .rxt instead of .txt
A

> > > from pathlib import Path
import os, shutil

> > > for filename in Path.home().glob(‘*.rxt’):
&raquo_space;>os.unlink(filename)

  1. Comment os.unlink and insert:
    &raquo_space;>print(filename)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a much better way for deleting files? What is the downside?

A

Using the send2trash module, to prevent premanent deletion

> > > import send2trash
send2trash.send2trash(‘spam.txt’)

Downside: Doesn´t free up disk space

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

How can you get to each file and subfolder of a folder easily? For example to rename them.

A

using the os.walk function, which always returns 3x values on each iteration, e.g.:

for folderName, subFolder, fileName in os.walk(path):
    print(folderName)
    for subfolders in subFolder:
          print(subfolders)
    for filenames in fileName:
          print(filenames)

The print functions can be replaced with any other custom code
Probably using os.path.join(sourcePath, filename) necessary

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