Skip to content

Instantly share code, notes, and snippets.

@akfheaven
Last active October 8, 2020 10:18
Show Gist options
  • Save akfheaven/95445cf39cce254d42d8d1cd83b6600d to your computer and use it in GitHub Desktop.
Save akfheaven/95445cf39cce254d42d8d1cd83b6600d to your computer and use it in GitHub Desktop.
frequency used file sloving python methods
import json
from collections import OrderedDict
import shutil
import os
import zipfile
import py7zr
# version: 1.0.0
# dir: folder
# file: not folder
# path: abs path of file or dir
# name: just the name of file or dir
# util function
def copy_dir(src_dir_path, dst_dir_path):
try:
shutil.copytree(src_dir_path, dst_dir_path)
except Exception as err:
print(err)
def del_dir(src_dir_path):
try:
shutil.rmtree(src_dir_path)
except Exception as err:
print(err)
def make_dir(dir_path):
try:
os.makedirs(dir_path)
except Exception as e:
# print(str(e))
pass
def move_path(src_path, dst_path):
try:
shutil.move(src_path, dst_path)
except Exception as err:
print(err)
def copy_file(src_file_path, dst_file_path):
try:
shutil.copy(src_file_path, dst_file_path)
except Exception as err:
print(err)
def del_file(file_path):
try:
os.remove(file_path)
except Exception as err:
print(err)
def zip_dir(dir_path, out_file_path, is_include_root_dir=True):
zip = zipfile.ZipFile(out_file_path, "w", zipfile.ZIP_DEFLATED)
rootDirName = ""
if is_include_root_dir is True:
rootDirName = os.path.split(dir_path)[1] + "/"
for path, dirnames, filenames in os.walk(dir_path):
# 去掉目标跟路径,只对目标(文件夹及)下边的文件和文件夹进行压缩
fpath = rootDirName + path.replace(dir_path, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
def unzip_zip(file_path, dst_dir_path, passwd):
zip_file = zipfile.ZipFile(file_path)
for name in zip_file.namelist():
print('extract:' + name)
zip_file.extract(name, dst_dir_path, pwd=passwd)
zip_file.close()
def unzip_7z(file_path, dst_dir_path, pwd):
with py7zr.SevenZipFile(file_path, mode='r', password=pwd) as zip_file:
print("open 7z file:" + file_path)
zip_file.extractall(path=dst_dir_path)
def unrar(file_path, dst_dir_path, pwd, auto_rename=False):
p_auto_name = ''
if auto_rename:
p_auto_name = '-or'
cmd = r'unrar -p' + pwd + r' -u '+ p_auto_name +' x "' + file_path + r'" "' + dst_dir_path + r'"'
# print(cmd)
os.system(cmd)
def list_dir(dir_path):
res = []
for file in os.listdir(dir_path):
res.append(file)
return res
def recursive_dir_with_op(dir_path, op):
for file in os.listdir(dir_path):
cur_name = dir_path + file
if not os.path.isdir(cur_name):
op(dir_path, file, True)
else:
isContinue = op(dir_path, file, False)
if isContinue:
recursive_dir_with_op(cur_name + '/', op)
def read_json(file_path):
json_dict = None
if os.path.exists(file_path):
with open(file_path, 'r', encoding='UTF-8') as f_json:
json_dict = json.load(f_json, object_pairs_hook=OrderedDict)
else:
print(file_path + " not exist")
return json_dict
def write_json(file_path, json_dict):
with open(file_path, 'w', encoding='UTF-8') as file:
json.dump(json_dict, file)
def write_file(file_path, lines):
with open(file_path, 'w', encoding='UTF-8') as file:
for line in lines:
file.write(line + '\n' )
def read_file(file_path):
lines = None
with open(file_path, 'r', encoding='UTF-8') as file:
lines = file.readlines()
return lines
@akfheaven
Copy link
Author

update 1.0.0 , add unzip function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment