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
人气:409 ℃ 时间:2020-03-30 00:19:38
解答
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()
推荐
猜你喜欢
- How many people are in your family Describe them 尽可能 多的 回答 摆脱了 急用
- Much to our____,the result of the competition is far better than expected. A.horror B.sadness
- 甲乙两车同时从AB两地相对开出,在距终点15千米处相遇.甲行完全程用5时,乙用4时,AB两地相距多少千米?
- 关于物体的浮力与大气压的问题:为什么漂浮在水上的物体的浮力等于重力,而不是重力加大气的压力呢 很...
- (英文翻译)他说他刚买了两张票.
- 集合A={x|x2+px+q=0},B={x|qx2+px+1=0},同时满足:(1)A∩B≠∮;(2)-2∈A(p,q≠
- a food list的意思
- 两船从甲乙两岸开出,第一次相遇在距甲岸700米处,继续前进,到对岸返回,再相遇在距乙岸400米处,求两岸距离