from openpyxl import * import os def modify_file(): file_list = os.listdir(cwd) for file in file_list: if os.path.isfile(file): print(f"公司:{file} 文件修改 >>> ", end="") wb = load_workbook(file) ws_name=wb.sheetnames[0] ws = wb[ws_name] ws.delete_cols(1,3) # 删除第 13 列数据 ws['A1'].comment = None wb.save(file) print(f"OK!") print("全部完成") if __name__ == '__main__': os.chdir("../files1") cwd = os.getcwd() dir_confirm = input(f"工作目录 {cwd},是否正确(y/n):") if dir_confirm == "y": modify_file()
################################################################
import os def move_file(): file_list = os.listdir(cwd) i = 0 for file in file_list: i += 1 try: city, file_ = file.split("_", maxsplit=1) except: continue else: os.rename(file, file_) print(f"第{i}个文件 市:{city} 公司:{file_} 文件重命名Ok >>> ", end="") if os.path.exists(city) and os.path.isdir(city): print(f"文件夹 {city} 已存在,移动文件 >>> ", end="") os.system(f"move {file_} {city}") print("OK >>> ", end="") else: print(f"建立 {city} 文件夹 >>> ", end="") os.mkdir(city) print("OK >>> ", end="") os.system(f"move {file_} {city}") print(f"完成!") print("全部完成") if __name__ == '__main__': os.chdir("../files1") cwd = os.getcwd() dir_confirm = input(f"工作目录 {cwd},是否正确(y/n):") if dir_confirm == "y": move_file()
################################################################
import os henan_city_list = ["郑州市", "开封市", "洛阳市", "平顶山市", "安阳市", "鹤壁市", "新乡市", "焦作市", "濮阳市", "许昌市", "漯河市", "三门峡市", "南阳市", "商丘市", "信阳市", "周口市", "驻马店市", "省直辖县级行政区划", ] def move_file(): file_list = os.listdir(cwd) i = 0 for file in file_list: if file != "河南省" and os.path.isdir(file): if file in henan_city_list: i += 1 print(f"第{i}个文件夹 {file} >>> ", end="") print(f"文件夹 {file} 移动 至 河南省 >>> ", end="") os.system(f"move {file} 河南省") print("OK >>> ", end="") print(f"完成!") else: continue else: continue print("全部完成") if __name__ == '__main__': os.chdir("../files") cwd = os.getcwd() dir_confirm = input(f"工作目录 {cwd},是否正确(y/n):") if dir_confirm == "y": move_file()
################################################################
import os def move_file(): file_list = os.listdir(cwd) i = 0 for file in file_list: i += 1 try: city, file_ = file.split("_", maxsplit=1) except: os.rename(file, f"失败-{file}") continue else: if file_.find(",") != -1: file_part_1, file_part_2 = file_.split(",", maxsplit=1) file_ = file_part_1 + "-" + file_part_2 os.rename(file, file_) print(f"第{i}个文件 市:{city} 公司:{file_} 文件重命名Ok >>> ", end="") if os.path.exists(city) and os.path.isdir(city): print(f"文件夹 {city} 已存在,移动文件 >>> ", end="") os.system(f"move {file_} {city}") print("OK >>> ", end="") else: print(f"建立 {city} 文件夹 >>> ", end="") os.mkdir(city) print("OK >>> ", end="") os.system(f"move {file_} {city}") print(f"完成!") else: os.rename(file, f"跳过-{file}") continue print("全部完成") if __name__ == '__main__': os.chdir("../files1") cwd = os.getcwd() dir_confirm = input(f"工作目录 {cwd},是否正确(y/n):") if dir_confirm == "y": move_file()