![](/img/trans.png)
[英]Create a new .txt file for each .pdf files in a directory in python
[英]Python: Create new files in a directory of choice, and in each file, write specific directory paths into it
因此,我正在尝试创建此功能,该功能将在选择的文件夹中创建多个文本文件(已解决)。
同时,该函数还接收一个单独嵌套文件夹的“目录路径”,并将这些目录路径写入从上面创建的那些文本文件中。
嵌套文件夹的结构如下:
- Image-dataset_1
- Station_1
- Sess_1
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Sess_2
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Station_2
- Sess_1
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Sess_2
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Station_3
- Sess_1
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Sess_2
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Station_4
- Sess_1
- Img1.jpg, Img2.jpg, Img3.jpg, ....
- Sess_2
- Img1.jpg, Img2.jpg, Img3.jpg, ....
但是,我被困在将正确的目录路径(没有图像文件)写入相应的文本文件的最后一步。 有人可以帮我解决这个问题吗? 我希望我的问题足够清楚,如果您需要更好的解释,请告诉我。
这是我当前的脚本:
def createFiles():
file_dir_input = str(input("Enter the absolute path of the directory where you want the 'batch-run-commands' "
"files to be saved in (end with '/'): "))
dataset_input = str(input("What is the name of the dataset that you would like to create files for? "))
numfile_input = int(input("How many stations are you creating the 'batch-run-commands' files for? "))
numsess_input = int(input("How many sessions per each station are there currently? "))
format_input = str(input("Which file format would you like the 'batch-run-commands' files to be (eg: '.json') ? "))
org_img_dir_input = str(input("Enter the absolute path of the 'top-parent' directory which the images are stored "
"in (end with '/'): "))
common_dirname_input = str(input("What is the common part in the names of the 'direct-parent' folders of the images"
" (eg: 'Session*'): "))
for path, dirs, files in os.walk(os.path.abspath(org_img_dir_input)):
for dirname in fnmatch.filter(dirs, common_dirname_input):
dirpath = os.path.join(path, dirname)
for i in range(0, numfile_input):
for j in range(0, numsess_input):
f = open(f"{file_dir_input}{dataset_input}{i + 1:02d}_{'Session'}{j + 1}{format_input}", "a")
f.write(f"{dirpath}/")
首先,将RAW用户输入作为文件名可能存在风险。 如果用户提供了类似的路径: ./../../../foo.bar
那么很多文件可以被覆盖。
其次,您不需要将输入转换为str
对象。
默认情况下input()
函数返回一个str
第三:如果用户提供了一个str
当你执行int(input())
时程序可能会崩溃
我的解决方案:(完整代码)
def create_files(cwd:str, paths:list, pattern:str, dataset_name:str):
import os
import shutil
import uuid
"""
Create files in the current working directory.
"""
for path in paths:
# Create the directory. if it already exists, overwrite it.
pth = os.path.join(cwd, dataset_name, path)
if os.path.isfile(pth):
os.remove(pth)
elif os.path.isdir(pth):
shutil.rmtree(pth, ignore_errors=True) # Force delete folder.
os.makedirs(pth)
current_sesson = 0
while True:
current_sesson += 1
pth_session = os.path.join(pth, str(current_sesson))
if os.path.isdir(pth_session):
current_sesson += 1
continue
else:
os.makedirs(pth_session)
break
# Create the files.
# Generate random file names.
file_name = uuid.uuid4().hex
file_name = os.path.join(pth_session, pattern.replace('*', file_name))
with open(file_name, 'w') as f:
f.write('Foo Bar')
如果我误解了提问,请原谅我。 很难理解你想要做什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.