1. File operations
ls 、 mk 、 rm 、 cp
[1] List folders
Result list = ls (full file path, option="", to include prefix=False) # option="-r" means recursive listing
【2】Create file or folder
mk (full file path, option="-p", to delete old files=False) # option="-p" means recursive creation, or -r
【3】Delete file or folder
rm (full file path, option="-rf") # option="-r" means recursive deletion
【4】Copy file or folder
cp (old file, new file, to delete old file=False)
2. File open mode
(1) Three basic modes: r, w, a
r read only
w Delete the original text and write only
a Keep the original text (additional), write only
(2) Additional suffix
b means binary read and write
+ Means additional read and write permissions are granted
U means that all line breaks read are changed to \n
(3) The difference between the three modes
When there is no file: r error, w, a new
Whether to clear the original text: r, a no, w yes
Initial reading position: the beginning of r, w, the end of a
import os
import shutil
from u_工具 import stream,getDeepFilePaths
# region fileSystem
def exist(文件全路径):
return os.path.exists(文件全路径)
def isdir(文件全路径):
if exist(文件全路径):
return os.path.isdir(文件全路径)
else:
文件后缀 = get文件后缀(文件全路径)
if not 文件后缀:
return True
else:
return False
def ls(文件全路径, 选项="", 要包含前缀=False):
选项 = 选项.lower()
if exist(文件全路径):
if isdir(文件全路径):
if ("p" in 选项) or ("r" in 选项):
return getDeepFilePaths(文件全路径, "*")
else:
if 要包含前缀:
return stream(os.listdir(文件全路径)) \
.map(lambda i: os.path.join(文件全路径, i)).collect()
else:
return os.listdir(文件全路径)
else:
return [文件全路径];
else:
return []
def mkdir(文件全路径, 选项="-p"):
选项 = 选项.lower()
if not exist(文件全路径):
if ("p" in 选项) or ("r" in 选项):
os.makedirs(文件全路径)
else:
os.mkdir(文件全路径)
def mk(文件全路径, 选项="-p", 要删除旧文件=False):
选项 = 选项.lower()
if exist(文件全路径):
if 要删除旧文件:
rm(文件全路径)
else:
return
if isdir(文件全路径):
mkdir(文件全路径, 选项)
else:
所在目录 = get文件所在目录(文件全路径)
if 所在目录 and (not exist(所在目录)):
mk(所在目录, 选项)
with open(文件全路径, "a"):
pass
def rm(文件全路径, 选项="-rf"):
if exist(文件全路径):
if isdir(文件全路径):
if ("p" in 选项) or ("r" in 选项):
shutil.rmtree(文件全路径)
else:
# 只删除文件,保留文件夹
try:
os.rmdir(文件全路径)
except:
stream(ls(文件全路径, 要包含前缀=True)).filter(lambda i: not isdir(i)) \
.forEach(lambda i: rm(i))
else:
os.remove(文件全路径)
def get文件后缀(文件全路径):
return os.path.splitext(文件全路径)[1]
def get文件名(文件全路径):
return os.path.basename(文件全路径)
def get文件所在目录(文件全路径):
return os.path.dirname(文件全路径)
def basename(文件全路径):
return get文件名(文件全路径)
def dirname(文件全路径):
return get文件所在目录(文件全路径)
def cp(旧文件, 新文件, 要删除旧文件=False):
旧文件类型 = "dir" if isdir(旧文件) else "file"
新文件类型 = "dir" if isdir(新文件) else "file"
def file_file():
# shutil.copyfile(旧文件,新文件) # 只复制内容
# 复制内容和权限 新文件不存在:新建,存在:覆盖
shutil.copy(旧文件, 新文件)
def file_dir():
if not exist(新文件):
mk(新文件)
shutil.copy(旧文件, 新文件)
def dir_file():
if not exist(新文件):
mk(新文件)
with open(新文件, "ab") as ff:
for i in ls(旧文件, 要包含前缀=True):
with open(i, "rb") as f:
ff.write(f.read())
def dir_dir():
shutil.copytree(旧文件, 新文件)
def default():
raise Exception("复制失败,参数类型未支持")
switch = {
"file-file": file_file,
"file-dir": file_dir,
"dir-file": dir_file,
"dir-dir": dir_dir
}
switch.get(f"{旧文件类型}-{新文件类型}", default)()
if 要删除旧文件:
rm(旧文件)
# endregion fileSystem
from u_tool import stream, getDeepFilePaths at: https://blog.csdn.net/u013595395/article/details/108772974