Created
June 15, 2022 14:10
-
-
Save fishyer/25dce66246740fa6b316717ea8918353 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from datetime import datetime | |
import json | |
path="C:/Users/Administrator/Dropbox/MyGithub/MyPython/src/FileUtil.py" | |
# 打印文件路径 | |
def printPath(path): | |
print(path) | |
print(os.path.dirname(path), ".dir") | |
print(os.path.basename(path), ".basename") | |
print(os.path.splitext(path)[0], ".stem") | |
print(os.path.splitext(path)[1], ".ext") | |
print(os.path.splitext(os.path.basename(path))[0]) | |
# 返回带后缀名的文件名,eg:2022-06-15.md | |
def getFileExt(path): | |
return os.path.basename(path) | |
# 返回文件的后缀名,可能为空,eg:.md | |
def getFileExt(path): | |
return os.path.splitext(path)[1] | |
# 返回文件所在的文件夹,eg:C:/Users/Administrator/Dropbox/MyGithub/MyPython/2022-06-15下午2_10_05导出葫芦笔记数据 | |
def getFileDir(path): | |
return os.path.dirname(path) | |
# 返回无后缀名的文件名,eg:2022-06-15 | |
def getFileName(path): | |
return os.path.splitext(os.path.basename(path))[0] | |
# 增量写入 | |
def writeStrAppend(val, path): | |
file = open(path, "a", encoding='utf-8') | |
file.write(val) | |
file.flush() | |
file.close() | |
print(f"增量写入文本到文件成功:{path}") | |
# 合并文件 | |
def mergeFile(src, dest): | |
print("mergeFile",src,dest) | |
s=readFile(src) | |
tf=datetime.now().strftime("%Y_%m_%d-%H_%M_%S") | |
content=f"\n\n## MergeFile-{tf}\n\n"+s | |
writeStrAppend(content,dest) | |
os.remove(src) | |
# 遍历文件夹 | |
def traverseDir(dir): | |
files = os.listdir(dir) | |
for file in files: | |
if not os.path.isdir(file): | |
f = open(f"{dir}/{file}") | |
name = os.path.splitext(os.path.basename(file))[0] | |
print(name) | |
# 遍历文件,一行行遍历,读取文本 | |
def readFileWithLines(file): | |
str = "" | |
iter_f = iter(file) | |
for line in iter_f: | |
str = str + line | |
return str | |
# 读取文件 | |
def readFile(path): | |
str = "" | |
with open(path, 'r', encoding='utf-8') as f: | |
str = f.read() | |
print(f"读取文件成功:{path}") | |
return str | |
# 写入文本到文件 | |
def writeStr(val, path): | |
if os.path.exists(path): | |
os.remove(path) | |
with open(path, 'w', encoding='utf-8') as file: | |
file.write(val) | |
print(f"写入文本到文件成功:{path}") | |
# 读取json到map | |
def read_map(path): | |
json_str = readFile(path) | |
return json.loads(json_str) | |
# 遍历文件夹,对每个文件都做一种操作 | |
def traverseDirWithProcess(dir, process): | |
files = os.listdir(dir) | |
if len(files)==0: | |
print("没有需要操作的文件",dir) | |
return | |
for file in files: | |
if not os.path.isdir(file): | |
# f = f"{dir}/{file}" | |
f=os.path.join(dir,file) | |
process(f) | |
# 写入map到文件,按json格式保存 | |
def writeMap(val, path): | |
file = open(path, "w", encoding='utf-8') | |
json_str = json.dumps( | |
val, indent=4, ensure_ascii=False, separators=(',', ':')) | |
file.write(json_str) | |
file.flush() | |
file.close() | |
print(f"写入map到文件成功:{path}") | |
# 写入文本到文件 | |
def writeStr(val, path): # 写入文本到文件 | |
if os.path.exists(path): | |
os.remove(path) | |
with open(path, 'w', encoding='utf-8') as file: | |
file.write(val) | |
print(f"写入文本到文件成功:{path}") | |
# 增量写入文本到文件 | |
def writeStrAppend(val, path): | |
file = open(path, "a", encoding='utf-8') | |
file.write(val) | |
file.flush() | |
file.close() | |
print(f"增量写入文本到文件成功:{path}") | |
# 增量写入文本到文件-在头部添加 | |
def writeStrAppendHead(val, path): | |
src = "" | |
if os.path.exists(path): | |
src = read_file(path) | |
file = open(path, "w") | |
content = val+"\n"+src | |
file.write(content) | |
file.flush() | |
file.close() | |
print(f"增量写入文本到文件头部成功:{path}") | |
# 写入列表到文件 | |
def writeList(val, path): | |
file = open(path, "w") | |
file.write('\n'.join(val)) | |
file.flush() | |
file.close() | |
print(f"写入列表到文件成功:{path}") | |
# 过滤掉Mac系统默认的.DS_Store文件 | |
def isnotDsStore(val): | |
if not re.match('^.*[.]DS_Store$', val): | |
return True | |
else: | |
return False | |
# printPath(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment