我想将空文本文件复制到我想要的地方

时间:2019-05-17 08:23:20

标签: import path pathlib

此工具制作了很多名为package的文件夹,我还想在每个package文件夹中制作名为dict.txt的空文本文件。

但是我是新手,我会尽我所能,但是失败了

我认为我必须使用pathlib模块或其他东西

如果您具体描述源代码和解释,我将不胜感激

谢谢!

import glob
import shutil
import os
import re
import sys

from pathlib import Path

dirs = ['DataGathering']

for dir in dirs:

    file_names = glob.glob (os.path.join (dir, '*'))
    file_names = [os.path.basename (name) for name in file_names]
    if not os.path.exists (dir + '_renamed'):
        os.makedirs (dir + '_renamed')
        # os.makedirs(dir+'_renamed/csv')
    root = dir + '_renamed'

for name in file_names:
    if name.endswith ('csv'):
        continue
    rename = re.sub ("[\[].*?[\]]","", name)
    print (name)

    splitted = rename.split ('_')
    package = splitted[2]
    array = splitted[0]
    splitted[0] = package
    splitted[2] = array
    pop_index = [-2] * 5 + [0]
    package = splitted[0]
    print (splitted)
    for index in pop_index:
        splitted.pop (index)

    if not os.path.exists (os.path.join (root, package)):
        os.makedirs (os.path.join (root, package))

    destination = '_'.join (splitted)
    print (splitted, destination)


    if not ('Ang' in name or 'K3D' in name or name.endswith ('csv')):
        shutil.copy(os.path.join (dir, name), os.path.join (root, package, destination))

0 个答案:

没有答案