-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHUtil_Test.py
More file actions
47 lines (35 loc) · 1.32 KB
/
Copy pathSHUtil_Test.py
File metadata and controls
47 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
import shutil
# This script demonstrates various file operations using the shutil module in Python.
# Directory and files operations
# Platform-dependent efficient copy operations
# copytree example
# rmtree example
# Archiving operations
# Archiving example
# Archiving example with base_dir
# Querying the size of the output terminal
# Create a directory
os.mkdir('example_dir')
# Create a file in the directory
with open('example_dir/example_file.txt', 'w', encoding='utf-8') as f:
f.write('This is an example file.')
# Copy a file
shutil.copy('example_dir/example_file.txt', 'example_dir/copied_file.txt')
# Copy a directory
shutil.copytree('example_dir', 'example_dir_copy')
# Move a file
shutil.move('example_dir/copied_file.txt', 'example_dir/moved_file.txt')
# Rename a file
os.rename('example_dir/moved_file.txt', 'example_dir/renamed_file.txt')
# Archive a directory (creates a zip file)
shutil.make_archive('example_dir_archive', 'zip', 'example_dir')
# Extract the archive
shutil.unpack_archive('example_dir_archive.zip', 'extracted_dir')
# Remove a directory tree
shutil.rmtree('example_dir')
shutil.rmtree('example_dir_copy')
shutil.rmtree('extracted_dir')
# Remove the archive
os.remove('example_dir_archive.zip')
print("All shutil operations completed successfully.")