![](/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.