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,首先使用基本目录,然后使用自定义目录名称(文件名可以作为第三个参数提供)。只是要非常小心地信任用户输入的任何东西,尤其是文件系统操作。