0

I have a folder which comprises of several pdfs and word files.I want to create a new folder where I want subfolders on the basis of the files which I read earlier and each sub folder should have 3 blank text files lets say test1.txt,test2.txt,test.txt I am new to python kindly help....

Ani
  • 147
  • 2
  • 14
  • 3
    https://stackoverflow.com/questions/47518669/create-new-folder-in-python-with-pathlib-and-write-files-into-it – ruslan_krivoshein Jan 27 '20 at 12:23
  • @ruslankrivoshein The above link you shared doesnt work.If there is any other way kindly let me know.I need to read a folder in which there are pdf and doc files present.On the basis of those file name i have to folders and in each folder random 3 text file should be present.... – Ani Jan 28 '20 at 03:32

1 Answers1

0
from pathlib import Path

files = Path().iterdir()

for file in files:
    new_file_path = Path(f'./{file.stem}')
    new_file_path.mkdir(parents=True, exist_ok=True)

    for i in range(3):
        p = new_file_path / f'{i + 1}.txt'
        with p.open('w') as nf:
             nf.write('')

I run this script within directory, where I have one.doc, two.doc and three.pdf, for instance. After that I get next:

one/
    1.txt
    2.txt
    3.txt
two/
    1.txt
    2.txt
    3.txt
three/
    1.txt
    2.txt
    3.txt
one.doc
two.doc
three.pdf

I hope, you are able to remove then unused files.