python创立并写入新文件,
python统计特定文件夹下的word和pdf的数量
import glob,os
path就是你说的特定文件夹
path = r"D:\linshi"
这里的pdf能够换成docx
file=glob.glob(os.path.join(path, "*.pdf"))
count = 0
for i in file:
count = count + 1
print(count)
复制文件的残缺门路借助python对该文件夹的文件批量复制到另一个指定文件夹中。有两种模式,一种只复制文件。第二种复制文件的残缺门路
import os
import shutil
def get_all_file_by_type(path, type=()): # 取得以type类型结尾的所有文件,返回一个list
filelist = []for a, b, c in os.walk(path): for name in c: fname = os.path.join(a, name) if fname.endswith(type): filelist.append(fname)return filelist
def get_all_file_by_string(path, string_list):
filelist = []for a, b, c in os.walk(path): for name in c: fname = os.path.join(a, name) for string in string_list: # 遍历string_list,如果文件门路中蕴含string,那么append进filelist if string in fname: # 如果只想要文件名符合条件,把fname换成name即可 filelist.append(fname) breakreturn filelist
def copy_file_by_type(old_path, new_path, type=('doc', 'docx'), requird_dir=False):
try: file_list = get_all_file_by_type(old_path, type=type) # 取得该门路下所有的type类型文件 if not os.path.exists(new_path): # 创立新的文件夹 os.makedirs(new_path) if not requird_dir: # 如果仅复制文件 for file in file_list: name = file.split("\\")[-1] # 取得文件名字 new_paths = os.path.join(new_path, name) # 与新门路拼接,取得残缺的新门路 shutil.copy(file, new_paths) print(new_paths + "胜利") if requird_dir: for file in file_list: name = file.split("\\")[-1] # 取得文件名字 new_paths = file.replace(old_path, new_path) # 将一个残缺门路中,开始的门路替换成新的门路 dir = new_paths.split(name)[0] [PerfectMoney下载](https://www.gendan5.com/wallet/PerfectMoney.html)# 取得文件夹门路 if not os.path.exists(dir): # 创立新文件夹 os.makedirs(dir) shutil.copy(file, new_paths) print(new_paths + "胜利")except Exception as e: print(e)
def copy_file_by_string(old_path, new_path, string_list, requird_dir=False):
try: file_list = get_all_file_by_string(old_path, string_list=string_list) # 与上述一样,只不过这里调用的是get_all_file_by_string办法 if not os.path.exists(new_path): os.makedirs(new_path) if not requird_dir: for file in file_list: name = file.split("\\")[-1] new_paths = os.path.join(new_path, name) shutil.copy(file, new_paths) print(new_paths + "胜利") if requird_dir: for file in file_list: name = file.split("\\")[-1] new_paths = file.replace(old_path, new_path) print(new_paths) dir = new_paths.split(name)[0] if not os.path.exists(dir): os.makedirs(dir) shutil.copy(file, new_paths) print(new_paths + "胜利")except Exception as e: print(e)
if name == '__main__':
old_path = r"F:\aaaa"new_path = r"F:\bbbb"list = ["面试", "口试", "题库", "题目"]copy_file_by_string(old_path=old_path, new_path=new_path, string_list=list, requird_dir=False)# type = ('docx','doc',"pdf","md")# copy_file_by_type(old_path=old_path, new_path=new_path, type=type, requird_dir=True)
python压缩多个文件到zip格局-zipfile包实例
pip install zipfile
file=r'D:\test.zip'
out_path=r'D:\files'
遍历files文件夹下的文件,压缩发送
zip_1=zipfile.ZipFile(file,'w')
for f in os.listdir(out_path): zip_1.write(os.path.join(out_path,f),f,zipfile.ZIP_DEFLATED)
zip_1.close()
python批量删除文件名_Python批量批改文件名
import os, re
while True:
keyword = input("请输出你要删除的字符串:")
if len(keyword)==0 or keyword.isspace():
print("字符串不能为空!")
else:
break
suffix = input("须要筛选的文件名后缀(Enter代表所有):")
fileNames = os.listdir() #获取当前目录下的所有文件
for file in fileNames:
check = os.path.join(os.path.abspath('.'),file)
if os.path.isfile(check):
if len(suffix)==0 or suffix.isspace():
if keyword in file:
print(file," -> ",file.replace(keyword,''))
os.rename(file,file.replace(keyword,''))
else:
用正则表达式匹配后缀名
if re.match('.+?.'+suffix+'$',file) != None and keyword in file:
print(file," -> ",file.replace(keyword,''))
os.rename(file,file.replace(keyword,''))