| 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 |
import sys, shutil, os, glob, subprocess |
| 30 |
|
| 31 |
_path_7zip = r"C:\Program Files\7-Zip\7z.exe" |
| 32 |
|
| 33 |
def norm(path): |
| 34 |
return os.path.realpath(os.path.normpath(os.path.expanduser(path))) |
| 35 |
|
| 36 |
def cp(src, dst, **kw): |
| 37 |
src_found = False |
| 38 |
for pth in glob.glob(norm(src)): |
| 39 |
if os.path.isfile(pth): |
| 40 |
shutil.copy(pth, norm(dst)) |
| 41 |
src_found = True |
| 42 |
elif os.path.isdir(pth): |
| 43 |
shutil.copytree(pth, norm(dst)) |
| 44 |
src_found = True |
| 45 |
if not src_found and not kw.get('quiet', False): |
| 46 |
print('Warning: %s not found.' % repr(src)) |
| 47 |
|
| 48 |
|
| 49 |
def mv(src, dst): |
| 50 |
for pth in glob.glob(norm(src)): |
| 51 |
shutil.move(pth, norm(dst)) |
| 52 |
|
| 53 |
def rm(*paths, **kw): |
| 54 |
quiet = kw.get('quiet', False) |
| 55 |
recursive = kw.get('recursive', False) |
| 56 |
if recursive: |
| 57 |
cwd = os.getcwd() |
| 58 |
listcwd = os.listdir('.') |
| 59 |
for path in paths: |
| 60 |
pths = glob.glob(norm(path)) |
| 61 |
if not (pths or quiet or recursive): |
| 62 |
print("Warning: %s not found, couldn't be removed." % path) |
| 63 |
for pth in pths: |
| 64 |
if os.path.isfile(pth): |
| 65 |
os.remove(pth) |
| 66 |
if recursive: |
| 67 |
for pth in listcwd: |
| 68 |
if os.path.isdir(pth): |
| 69 |
cd(pth) |
| 70 |
rm(*paths, **kw) |
| 71 |
cd(cwd) |
| 72 |
|
| 73 |
def rename(src, dst): |
| 74 |
os.rename(norm(src), norm(dst)) |
| 75 |
|
| 76 |
def rmdir(*paths, **kw): |
| 77 |
quiet = kw.get('quiet', False) |
| 78 |
for path in paths: |
| 79 |
pths = glob.glob(norm(path)) |
| 80 |
if not (pths or quiet): |
| 81 |
print("Warning: %s not found, couldn't be removed." % path) |
| 82 |
for pth in pths: |
| 83 |
if os.path.isdir(pth): |
| 84 |
shutil.rmtree(norm(pth)) |
| 85 |
|
| 86 |
def mkdir(path): |
| 87 |
os.mkdir(norm(path)) |
| 88 |
|
| 89 |
def cd(path): |
| 90 |
return os.chdir(norm(path)) |
| 91 |
|
| 92 |
def command(string, quiet=False): |
| 93 |
out = subprocess.Popen(string, shell=True, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout |
| 94 |
output = out.read() |
| 95 |
sys.stdout.write(output) |
| 96 |
out.close() |
| 97 |
if not quiet: |
| 98 |
print("Command '%s' was executed." % string) |
| 99 |
return output |
| 100 |
|
| 101 |
def zip7(string): |
| 102 |
command('"%s" %s' %(_path_7zip, string)) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def append(srcs, dst): |
| 108 |
with open(dst, 'wb') as dest: |
| 109 |
for src in srcs: |
| 110 |
with open(src, 'rb') as source: |
| 111 |
dest.write(source.read()) |
| 112 |
|
| 113 |
def pause(string = "\n-- pause --\n"): |
| 114 |
input(string) |
| 115 |
|
| 116 |
def ls(path = '.'): |
| 117 |
if '*' in path: |
| 118 |
return glob.glob(path) |
| 119 |
return os.listdir(norm(path)) |