0

Can somebody tell me what to modify here, so the file I'm uploading can be uploaded from a subfolder? At the moment the files are stored in the same folder as the python script.

This script helps me to automate the process of adding ads to a site. Here is the part which uploads image files from a json file (photofiles is an array in config.json). I want to store the photos in a subfolder.

# Upload images
fileup = driver.find_element_by_xpath("//input[@type='file']")

for path in photofiles:
    uploaded_count = len(driver.find_elements_by_class_name("imagebox-thumbnail"))
    fileup.send_keys(os.path.abspath(path))
    total_upload_time = 0
    while uploaded_count == len(driver.find_elements_by_class_name("imagebox-thumbnail")) and \
                    total_upload_time < 30:
        time.sleep(0.5)
        total_upload_time += 0.5

    log.debug("Uploaded file in %s seconds." % total_upload_time)

2 Answers2

0

Should be pretty simple by just modifying the photofiles list. Assuming your code creates a list of files and stores it as "photofiles" you just need to append to it all the needed paths from the folder you want.

There are multiple ways of traversing a folder and, for example, getting all the files with a certain extension (see Find all files in a directory with extension .txt in Python). You could build your "photofiles" list from all the files with certain extensions in your desired folder.

BoboDarph
  • 2,751
  • 1
  • 10
  • 15
  • 1
    Hi, @BoboDarph. I think, He just wants to get inside the sub-dir. His lines :- ***"Can somebody tell me what to modify here, so the file I'm uploading can be uploaded from a subfolder? At the moment the files are stored in the same folder as the python script."*** – 0x48piraj Jul 04 '17 at 14:27
  • quick and dirty, as @BoboDarph suggested, I modified my config.json like this `"photofiles": [ "subfolder/photo.jpg" ]` and it worked. I will try now to modify my python script ... – Daniel Höfter Jul 04 '17 at 15:30
0

The file you are uploading can be easily uploaded from a subfolder.

>>> import os 
>>> print(os.getcwd())
C:\Python32 
>>> os.chdir('/test') 
>>> print(os.getcwd()) 
C:\test

Just use os.getcwd() and os.getcwd() to enter in sub-dir.

That's it. I think now, you will yourself implement the above snippet in your script.

BTW, I recommend you to visit Python Files.

0x48piraj
  • 395
  • 3
  • 12