Python 如何基于特定文件夹中的文件创建文件夹

Python 如何基于特定文件夹中的文件创建文件夹,python,pandas,text,directory,mkdir,Python,Pandas,Text,Directory,Mkdir,我有一个文件夹,它由几个PDF和word文件组成。我想创建一个新文件夹,其中我希望在前面阅读的文件的基础上创建子文件夹,每个子文件夹应该有3个空白文本文件,比如test1.txt、test2.txt、test.txt 我是python新手,请帮忙 from pathlib import Path files = Path().iterdir() for file in files: new_file_path = Path(f'./{file.stem}') new_file

我有一个文件夹,它由几个PDF和word文件组成。我想创建一个新文件夹,其中我希望在前面阅读的文件的基础上创建子文件夹,每个子文件夹应该有3个空白文本文件,比如test1.txt、test2.txt、test.txt 我是python新手,请帮忙

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('')
我在目录中运行这个脚本,例如在目录中有
one.doc
two.doc
three.pdf
。在那之后,我得到下一个:

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

我希望您能够删除未使用的文件。

@ruslankrivoshein您共享的上述链接不起作用。如果有任何其他方法,请告诉我。我需要阅读一个文件夹,其中有pdf和doc文件。根据这些文件名,我必须创建文件夹,每个文件夹中随机有3个文本文件。。。。