Create a text file in the desired path?

Asked 2 years ago, Updated 2 years ago, 118 views

import glob 

import shutil

import os

import re


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
        print(name)
        rename = re.sub("[\(\[].*?[\)\]]", "",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))

If you look at the code above, a folder called dataghering_renamed is created, and several folders are created with the value entered in the package, and each of them is created in the folder I want to create all the text files called dict.

For example,

makeTxt = os.path.join ({root, package}, "dict.txt")

make = open (makeTxt, "w")

I don't care about the contents in the dict on that path, but I want to create an empty text file I don't know what to do. The example is just what I thought (of course there's an error) and I'm trying to figure out the right way I'd appreciate it if you could teach me.

memo import

2022-09-22 16:52

1 Answers

It's hard to understand because there's no purpose in what you're going to do

*Nix allows you to create empty files with touch.

~$ touch empty.txt

With Python code, you can do it as follows.

In [1]: import pathlib                                                          

In [2]: pathlib.Path('empty.txt').touch()


2022-09-22 16:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.