这种问题其实才是 chatgpt 的正确用途: 提问“编写 python 程序,获取压缩文件 test.zip 中的文件夹名 name, 要求不进行解压,然后把 test.zip 重命名为 name.zip” import zipfile import os def get_zip_folder_name(file_path): with zipfile.ZipFile(file_path, 'r') as zip_file: for file_info in zip_file.infolist(): if file_info.is_dir(): return os.path.splitext(file_info.filename)[0] return None def rename_zip_file(file_path, new_file_name): os.rename(file_path, new_file_name) if __name__ == '__main__': zip_file_path = 'test.zip' folder_name = get_zip_folder_name(zip_file_path) if folder_name: new_file_name = folder_name + '.zip' rename_zip_file(zip_file_path, new_file_name) else: print('No folder found in the zip file.')