-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell
37 lines (29 loc) · 981 Bytes
/
shell
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
#
# Example file for working with filesystem shell methods
#
import os
from os import path
import shutil
from shutil import make_archive
from zipfile import ZipFile
def main():
# make a duplicate of an existing file
if path.exists("textfile.txt"):
# get the path to the file in the current directory
src = path.realpath("textfile.txt")
# let's make a backup copy by appending "bak" to the name
dst = src + ".bak"
# copy over the permissions, modification times, and other info
# shutil.copy(src, dst)
# shutil.copystat(src, dst)
# rename the original file
# os.rename("textfile.txt", "newfile.txt")
# now put things into a ZIP archive
# root_dir, tail = path.split(src)
# shutil.make_archive("archive","zip",root_dir)
# more fine-grained control over ZIP files
with ZipFile("testzip.zip", "w") as newzip:
newzip.write("textfile.txt")
newzip.write("textfile.txt.bak")
if __name__ == "__main__":
main()