-1

What I have is an initial directory with a file inside D:\BBS\file.x and multiple .txt files in the work directory D:\

What I am trying to do is to copy the folder BBS with its content and incrementing it's name by number, then copy/move each existing .txt file to the newly created directory to make it \BBS1, \BBS2, ..., BBSn (depends on number of the txt).

Visual example of the Before and After:

Initial view of the \WorkFolder

Work directory content before

Desired view of the \WorkFolder

Work directory content before the script (desired)

Right now I have reached only creating of a new directory and moving txt in it but all at once, not as I would like to. Here's my code:

from pathlib import Path
from shutil import copy
import shutil
import os

wkDir = Path.cwd()
src = wkDir.joinpath('BBS')
count = 0

for content in src.iterdir():
    addname = src.name.split('_')[0]
    out_folder = wkDir.joinpath(f'!{addname}')
    out_folder.mkdir(exist_ok=True)
    out_path = out_folder.joinpath(content.name)
    copy(content, out_path)

files = os.listdir(wkDir)

for f in files:
    if f.endswith(".txt"):
        shutil.move(f, out_folder)

I kindly request for assistance with incrementing and copying files one by one to the newly created directory for each as mentioned.

Not much skills with python in general. Python3 OS Windows Thanks in advance

  • I don't understand what is the actual problem. Could you elaborate? So the problem is that you want to separate the folder copying from the txt file copying? Am I misunderstanding the question? Could you maybe add a before and after sample of your file tree explaining what is not functioning in the after case? – schilli Jun 05 '21 at 12:53
  • @schilli thanks for your clarification. I have provided the images in the post itself. Feel free to ask more if not clear. – Alexander Larionov Jun 05 '21 at 13:14

1 Answers1

1

Now, I understand what you want to accomplish. I think you can do it quite easily by only iterating over the text files and for each one you copy the BBS folder. After that you move the file you are currently at. In order to get the folder_num, you may be able to just access the file name's characters at the particular indexes (e.g. f[4:6]) if the name is always of the pattern TextXX.txt. If the prefix "Text" may vary, it is more stable to use regular expressions like in the following sample.

Also, the function shutil.copytree copies a directory with its children.

import re
import shutil
from pathlib import Path

wkDir = Path.cwd()
src = wkDir.joinpath('BBS')

for f in os.listdir(wkDir):
    if f.endswith(".txt"):
        folder_num = re.findall(r"\d+", f)[0]
        target = wkDir.joinpath(f"{src.name}{folder_num}")

        # copy BBS
        shutil.copytree(src, target)

        # move .txt file
        shutil.move(f, target)
schilli
  • 1,700
  • 1
  • 9
  • 17
  • this is really running well. From now I see and have the example how to deal with the logic. I was thinking a bit more complicated over this. By the way, many thanks for demonstrating me the way to make it simple. Kindest regards – Alexander Larionov Jun 06 '21 at 04:31
  • You're very welcome. Often with simple tasks like copying files, when you end up with quite a complicated solution or feel like it's more complicated than the task, step back from the code and rethink if it may be done another way, because in python often there are quite nice simple solutions and the standard library of python is a powerful tool. – schilli Jun 06 '21 at 10:42