I don't know how to use baiduhi.Can you tell me how to solve the following problem?假设有个目录下有很多个文件夹,分为两类 A和B,A里面都是.txt文档,B里面又有文件夹,二文件夹 下才是.txt 怎样把A中.txt单独压缩在A文件夹里,B中以则子文件夹为单位压缩在B中Assuming that a directory has many folders.There are two types of these folders,A and B.In the A ,all the documents are .txt documents.But B has lots of folders like A.How to put .TXT compression alone in A and put subfolders compression in the B

问题描述:

I don't know how to use baiduhi.Can you tell me how to solve the following problem?
假设有个目录下有很多个文件夹,分为两类 A和B,A里面都是.txt文档,B里面又有文件夹,二文件夹 下才是.txt 怎样把A中.txt单独压缩在A文件夹里,B中以则子文件夹为单位压缩在B中
Assuming that a directory has many folders.There are two types of these folders,A and B.
In the A ,all the documents are .txt documents.But B has lots of folders like A.
How to put .TXT compression alone in A
and put subfolders compression in the B

Gotha,wait a moment,I'm writing it.
This code is written according to the cases you just described
the APath and BPath should be the path of A and B folder.
#
import tarfile
import os
APath = 'A'
dirA = os.walk(APath).next()[2]
tarA = tarfile.open('A/txt.tar.bz','w:bz2')
for file in dirA:
tarA.add(os.path.join(APath,file))
tarA.close()
BPath = 'B'
dirB = os.walk(BPath).next()[1]
os.chdir('B')
for directory in dirB:
tar = tarfile.open(directory+'tar.bz','w:bz2')
tar.add(directory)
tar.close()