finished ch9
This commit is contained in:
19
practice_projects/deleting_unneeded_files.py
Normal file
19
practice_projects/deleting_unneeded_files.py
Normal file
@@ -0,0 +1,19 @@
|
||||
#!python3
|
||||
# deleting_unneeded_files.py - walks through a folder tree and searches for exceptionally large files or folders. say,
|
||||
# ones that have a file size of more than 100MB. Print these files with their absolute path to the screen.
|
||||
|
||||
import os
|
||||
import shutil
|
||||
|
||||
def deleting_large_files(fd, mega):
|
||||
for foldername, subfolders, filenames in os.walk(fd):
|
||||
for filename in filenames:
|
||||
filepath = os.path.join(foldername, filename)
|
||||
size = os.path.getsize(filepath)
|
||||
size /= 1024 * 1024
|
||||
if size > mega:
|
||||
print(filepath)
|
||||
#os.unlink(filepath)
|
||||
|
||||
|
||||
deleting_large_files("D:\\BaiduNetdiskDownload", 10)
|
||||
72
practice_projects/filling_in_the_gaps.py
Normal file
72
practice_projects/filling_in_the_gaps.py
Normal file
@@ -0,0 +1,72 @@
|
||||
#!python3
|
||||
# filling_in_the_gaps.py - finds all files with a given prefix, such as
|
||||
# `spam001.txt`, spam002.txt, and so on, in a single folder and locates any
|
||||
# gaps in the numbering. Have the program rename all the later files to close
|
||||
# this gap.
|
||||
# write another program that can insert gaps into numbered files so that a new
|
||||
# file can be added.
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import re
|
||||
import random
|
||||
import pathlib
|
||||
|
||||
|
||||
def gengap(fd, prefix, n):
|
||||
os.chdir(fd)
|
||||
for f in os.listdir('.'):
|
||||
os.unlink(f)
|
||||
|
||||
smp = random.sample(range(1, 100), n)
|
||||
for idx in smp:
|
||||
newfile = os.path.join('.', prefix + str(idx).zfill(3) + '.txt')
|
||||
pathlib.Path(newfile).touch()
|
||||
|
||||
|
||||
def fillgap(fd, prefix):
|
||||
os.chdir(fd)
|
||||
reg = re.compile(r'^%s(\d+)' % prefix)
|
||||
n = -1
|
||||
sz = -1
|
||||
for filename in os.listdir('.'):
|
||||
mo = reg.search(filename)
|
||||
if mo is not None:
|
||||
if n == -1 and sz == -1:
|
||||
n = int(mo.group(1))
|
||||
sz = len(str(mo.group(1)))
|
||||
else:
|
||||
n += 1
|
||||
newname = prefix + str(n).zfill(sz) +
|
||||
os.path.splitext(filename)[1]
|
||||
shutil.move(filename, newname)
|
||||
|
||||
|
||||
def insertgap(fd, prefix):
|
||||
os.chdir(fd)
|
||||
reg = re.compile(r'^%s(\d+)' % prefix)
|
||||
for filename in os.listdir('.'):
|
||||
mo = reg.search(filename)
|
||||
if mo is not None:
|
||||
n = int(mo.group(1))
|
||||
sz = len(str(mo.group(1)))
|
||||
newfile = os.path.join('.', prefix + str(n).zfill(sz) +
|
||||
os.path.splitext(filename)[1])
|
||||
while os.path.exists(newfile):
|
||||
n += 1
|
||||
newfile = os.path.join('.', prefix + str(n).zfill(sz) +
|
||||
os.path.splitext(filename)[1])
|
||||
pathlib.Path(newfile).touch()
|
||||
break
|
||||
|
||||
|
||||
curpath = os.getcwd()
|
||||
|
||||
# os.chdir(curpath)
|
||||
# gengap('test', 'spam', 9)
|
||||
|
||||
# os.chdir(curpath)
|
||||
# insertgap('test', 'spam')
|
||||
|
||||
os.chdir(curpath)
|
||||
fillgap('test', 'spam')
|
||||
28
practice_projects/selective_copy.py
Normal file
28
practice_projects/selective_copy.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!python3
|
||||
# selective_copy.py - walks through a folder tree and searches for files with a
|
||||
# certain file extension (such as .pdf or .jpg). Copy these files from whatever
|
||||
# location they are in to a new folder.
|
||||
|
||||
import os
|
||||
import shutil
|
||||
|
||||
|
||||
def selective_copy(flr, ext, new_flr):
|
||||
if not os.path.exists(new_flr):
|
||||
os.mkdir(new_flr)
|
||||
base = os.path.basename(new_flr)
|
||||
|
||||
for foldername, subfolders, filenames in os.walk(flr):
|
||||
new_base = os.path.basename(foldername)
|
||||
if base == new_base:
|
||||
continue
|
||||
|
||||
for filename in filenames:
|
||||
if filename.endswith(ext):
|
||||
filepath = os.path.join(foldername, filename)
|
||||
new_filepath = os.path.join(new_flr, filename)
|
||||
shutil.move(filepath, new_filepath)
|
||||
|
||||
|
||||
selective_copy("C:\\Users\\pezy\\Downloads", ('.exe', '.msi'),
|
||||
"C:\\Users\\pezy\\Downloads\\Software")
|
||||
0
practice_projects/test/spam016.txt
Normal file
0
practice_projects/test/spam016.txt
Normal file
0
practice_projects/test/spam017.txt
Normal file
0
practice_projects/test/spam017.txt
Normal file
0
practice_projects/test/spam018.txt
Normal file
0
practice_projects/test/spam018.txt
Normal file
0
practice_projects/test/spam019.txt
Normal file
0
practice_projects/test/spam019.txt
Normal file
0
practice_projects/test/spam020.txt
Normal file
0
practice_projects/test/spam020.txt
Normal file
0
practice_projects/test/spam021.txt
Normal file
0
practice_projects/test/spam021.txt
Normal file
0
practice_projects/test/spam022.txt
Normal file
0
practice_projects/test/spam022.txt
Normal file
0
practice_projects/test/spam023.txt
Normal file
0
practice_projects/test/spam023.txt
Normal file
0
practice_projects/test/spam024.txt
Normal file
0
practice_projects/test/spam024.txt
Normal file
0
practice_projects/test/spam025.txt
Normal file
0
practice_projects/test/spam025.txt
Normal file
@@ -8,3 +8,4 @@
|
||||
- [Chapter 6 – Manipulating Strings](ch06/README.md)
|
||||
- [Chapter 7 – Pattern Matching with Regular Expressions](ch07/README.md)
|
||||
- [Chapter 8 – Reading and Writing Files](ch08/README.md)
|
||||
- [Chapter 9 – Organizing Files](ch09/README.md)
|
||||
|
||||
17
practice_questions/ch09/README.md
Normal file
17
practice_questions/ch09/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Chapter 9 – Organizing Files
|
||||
|
||||
> Q: 1. What is the difference between `shutil.copy()` and `shutil.copytree()`?
|
||||
|
||||
`shutil.copy()` copies the file, `shutil.copytree()` copies the directory.
|
||||
|
||||
> Q: 2. What function is used to rename files?
|
||||
|
||||
`shutil.move()`
|
||||
|
||||
> Q: 3. What is the difference between the delete functions in the `send2trash` and `shutil` modules?
|
||||
|
||||
`send2trash` will move file to recycle bin, while `shutil` functions will permanently delete files and folders.
|
||||
|
||||
> Q: 4. `ZipFile` objects have a `close()` method just like `File` objects’ `close()` method. What `ZipFile` method is equivalent to `File` objects’ `open()` method?
|
||||
|
||||
`zipfile.ZipFile()`.
|
||||
Reference in New Issue
Block a user