使用rar文件的名称解压缩文件



我有一个文件夹,里面有4个rar(zipped(文件。

截图#1:rar文件和名称

这是打开它们的python代码:

#!/usr/bin/python
import os, zipfile, pyunpack, sys
import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import askdirectory
root = tk.Tk()
root.withdraw()
basis_folder = askdirectory(title='Select Folder')

for root, dirs, files in os.walk(basis_folder):
for filename in files:
if filename.endswith(".rar") :
print('RAR:'+os.path.join(root,filename))
elif filename.endswith(".zip"):
print('ZIP:'+os.path.join(root,filename))
name = os.path.splitext(os.path.basename(filename))[0]
if filename.endswith(".rar") or filename.endswith(".zip"):
try:
arch = pyunpack.Archive(os.path.join(root,filename))
# os.mkdir(name)
arch.extractall(directory=root)
os.remove(os.path.join(root,filename))
except Exception as e:
print("ERROR: BAD ARCHIVE "+os.path.join(root,filename))
print(e)
try:
# os.path.join(root,filename)os.remove(filename)
pass
except OSError as e: # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
sys.exit()
os._exit(0)

在unrar过程之后,代码删除rar文件并提取rar文件中的文件。我想将文件重命名为从rarfile1重命名为file1,从rarfile2重命名为file2,从rarfile重命名为file3,从rarfile1重命名为file1,从rarfile2重命名为文件1,依此类推…

这就是我想要的结果:

[截图#2:unrar文件及其新名称]

/root/
- example1.rar
- example2.rar

实现这一目标的一种方法是采取这些步骤,但我相信肯定有更好的方法:

  1. 临时创建一个与rar文件同名的目录。(用于/root/example1.rar文件的目录
  2. 将Rar文件移动到临时目录/root/example1/example1.rar
  3. 将example1.rar文件中的所有文件提取到example1目录中
/root/
example1/
file1.ext
file2.ext
example1.rar
  1. 删除rar文件
/root/    
example1/
file1.ext
file2.ext
  1. 将example1目录中的所有文件重命名为example1_file1.ext
/root/
/example1/
example1_file1.ext
example1_file2.ext
  1. 将所有文件移动到/root/
/root/
/example1/
example1_file1.ext
example1_file2.ext
  1. 删除/example1/目录:
/root/
example1_file1.ext
example1_file2.ext
  1. 以这种方式循环所有Rar。最终结果:

    /root/
    example1_file1.ext
    example1_file2.ext
    example2_file1.ext
    example2_file2.ext
    

我还没有运行和验证代码,但您的代码将按照以下行进行修改:

for root, dirs, files in os.walk(basis_folder):
for filename in files:
if filename.endswith(".rar"):
print('RAR:' + os.path.join(root, filename))
elif filename.endswith(".zip"):
print('ZIP:' + os.path.join(root, filename))
name = os.path.splitext(os.path.basename(filename))[0]
if filename.endswith(".rar") or filename.endswith(".zip"):
try:
# root/yourfile.rar
curr_file_path = os.path.join(root, filename)
# root/yourfile/
new_file_dir = curr_file_path.split('.')[0]
# root/yourfile/yourfile.rar
new_file_path = os.path.join(new_file_dir, filename)
os.mkdir(new_file_dir)
os.replace(curr_file_path, new_file_path)
arch = pyunpack.Archive(new_file_path)
# extract files to root/yourfile
arch.extractall(directory=new_file_dir)
# Remove the Rar file
os.remove(new_file_path)
files_in_curr = [f for f in os.listdir(new_file_dir) if os.path.isfile(f)]
for file in files_in_curr:
new_name = os.path.join(new_file_dir, filename.split('.')[0] + '_' + file)
# Rename file from root/yourfile/file1.ext to root/yourfile/yourfile_file1.ext
os.rename(os.path.join(new_file_dir, file), new_name)
# move file from root/yourfile/yourfile_file1.ext to root/yourfile_file1.ext
os.replace(os.path.join(new_file_dir, new_name), os.path.join(root, new_name))
# Remove the temporary empty directory
os.rmdir(new_file_dir)
except Exception as e:
print("ERROR: BAD ARCHIVE " + os.path.join(root, filename))
print(e)
try:
# os.path.join(root,filename)os.remove(filename)
pass
except OSError as e:  # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT:  # errno.ENOENT = no such file or directory
raise  # re-raise exception if a different error occured
sys.exit()
os._exit(0)

相关内容

  • 没有找到相关文章

最新更新