Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1e527ff

Browse files
committed
initial commit
1 parent e086e9f commit 1e527ff

File tree

10 files changed

+95
-3
lines changed

10 files changed

+95
-3
lines changed

‎C9_backing_up_a_folder.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# backupToZip.py - Copies an entire folder and its contents into
2+
# a ZIP file whose filename increments.
3+
4+
import os
5+
import zipfile
6+
7+
8+
def backup_to_zip(folder):
9+
# Backup the entire contents of "folder" into a ZIP file.
10+
folder = os.path.abspath(folder)
11+
12+
# Figure out the filename this code should use based on
13+
# what files already exist.
14+
number = 1
15+
while True:
16+
zip_file_name = os.path.basename(folder) + "_" + str(number) + ".zip"
17+
if not os.path.exists(zip_file_name):
18+
break
19+
number += 1
20+
21+
# Create the ZIP file.
22+
print("Creating %s..." % zip_file_name)
23+
backup_zip = zipfile.ZipFile(zip_file_name, 'w')
24+
25+
# Walk the entire folder tree and compress the files in each folder.
26+
for foldername, subfolders, filenames in os.walk(folder):
27+
print("Adding files in %s..." % foldername)
28+
# Add the current folder to the ZIP file.
29+
backup_zip.write(foldername)
30+
31+
# Add all the files in this folder to the ZIP file.
32+
for filename in filenames:
33+
newBase = os.path.basename(folder) + '_'
34+
if filename.startswith(newBase) and filename.endswith('.zip'):
35+
continue
36+
backup_zip.write(os.path.join(foldername, filename))
37+
backup_zip.close()
38+
print('Done!')
39+
40+
41+
backup_to_zip(".\\files\\C9_backup")

‎C9_deleting_uneeded_files.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
Program that walks through a folder tree and searches for exceptionally
3+
large files or folders—say, ones that have a file size of more than
4+
100MB.
5+
6+
"""
7+
8+
import os
9+
10+
11+
def delete_files(folder):
12+
folder = os.path.abspath(folder)
13+
14+
for folder, subfolders, files in os.walk(folder):
15+
for filename in files:
16+
file_size = os.path.getsize(os.path.join(folder, filename))
17+
if file_size < 10000000:
18+
continue
19+
# os.unlink(filename)
20+
print("Deleting " + filename + "... with size " + str(file_size))
21+
22+
23+
delete_files(".\\files")

‎C9_selective_copy.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Program that walks through a folder tree and searches for files with
3+
a certain file extension (.txt). Copy these files from whatever
4+
location they are in to a new folder.
5+
6+
"""
7+
8+
import os
9+
import shutil
10+
11+
12+
def selective_copy(folder, ext):
13+
"""
14+
Perform selective copy of all files in folder tree, that has specified extension.
15+
16+
:param folder: Folder tree to be copied
17+
:param ext: File extension that will be copied
18+
"""
19+
os.chdir(folder)
20+
if not os.path.exists('..\\new_dir'):
21+
os.makedirs('..\\new_dir')
22+
23+
for folder_name, subfolders, filenames in os.walk('.'):
24+
for filename in filenames:
25+
if not filename.endswith(ext):
26+
continue
27+
file_path = os.path.join(os.path.abspath(folder_name), filename)
28+
shutil.copy(file_path, '..\\new_dir\\')
29+
30+
31+
selective_copy('.\\files\\', '.txt')
File renamed without changes.

‎files/c9_remove_zeros/35.txt

Whitespace-only changes.

‎files/c9_remove_zeros/another/hereis.txt

Whitespace-only changes.

‎files/dir1/dildo2.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎files/dir1/testtt/da_da.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎files/dir1/vibrator.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /