Ro_nair
Ro_nair

Reputation: 77

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

I have a python script which runs to take screenshots in a loop and save all those images. However currently, all images are stored in Python35 folder.

Therefore I want the user to enter a name for a folder and create one by this name.

I know to use makedirs function and specify a path by newpath=r'C:/users/../' .

But how to append this new folder name specified by the user to the path?

(EDIT) Here is the Code I have:-

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

Upvotes: 0

Views: 3250

Answers (1)

kungphu
kungphu

Reputation: 4859

If you mean building the path at which the file should be saved, use os.path.join with the base directory first and the custom directory name second (the filename can be provided as a third argument). Just be very careful about trusting user input for anything, especially filesystem manipulation.

Upvotes: 3

Related Questions