如何根据特定文件夹中的文件创建文件夹
How to create Folder on the basis of files in a specific folder
我有一个包含多个 pdf 和 word 的文件夹 files.I 想在我之前阅读的文件的基础上创建一个新文件夹,其中我想要子文件夹,每个子文件夹应该有 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
希望您能够删除未使用的文件。
我有一个包含多个 pdf 和 word 的文件夹 files.I 想在我之前阅读的文件的基础上创建一个新文件夹,其中我想要子文件夹,每个子文件夹应该有 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
希望您能够删除未使用的文件。