簡體   English   中英

Python-如何使用用戶輸入的名稱創建文件夾?

[英]Python - How to create a folder with a user entered name?

我有一個python腳本,可以運行以循環獲取屏幕截圖並保存所有這些圖像。 但是當前,所有圖像都存儲在Python35文件夾中。

因此,我希望用戶輸入一個文件夾的名稱,並以此名稱創建一個。

我知道使用makedirs函數並通過newpath=r'C:/users/../'指定路徑。

但是,如何將用戶指定的此新文件夾名稱附加到路徑中?

(編輯)這是我的代碼:-

import openpyxl
import re
import os 
import time
from PIL import ImageGrab
import webbrowser

source_excel=input('Enter the source excel file name-> ')
wb=openpyxl.load_workbook(source_excel)
sheet=wb.active


strt=int(input('Enter starting range-> '))
end=int(input('Enter ending range-> '))

#NEED THE CHANGE HERE

foldname=input("Enter a folder name to create where images will be saved")
newpath=r'C:\Users\rnair\AppData\Local\Programs\Python\Python35\'demofile 


# Loop to get the link from excel and take the screenshot by opening the browser for each link

for q in range(strt,end):
    getme=q,sheet.cell(row=q,column=1).value        #get link from excel file       
    ab=getme[1]
    #myopener=Myopen()
    rowc=str(strt)
    url=ab
    webbrowser.open(url)                            #opens browser for the link fetched

    time.sleep(7)                                   # Delay to wait for the page to load completely
    im=ImageGrab.grab()
    os.chdir(newpath)
    im.save("Row"+rowc+".jpg","JPEG")               # Saving image to file
    strt=strt+1

如果要構建用於保存文件的路徑,請首先使用os.path.join並在其基礎目錄中使用os.path.join ,然后在其自定義目錄名稱中使用(文件名可以作為第三個參數提供)。 只是要非常信任用戶輸入的任何內容, 尤其是文件系統操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM