1

I wrote a python script to upload a video file into web portal using selenium library . Our video file name keep changing every hour. program A batch job creates a video with current date and writes to current working directory . Program b ( my python script) needs to upload that to website.

file_input = browser.find_element(By.XPATH, '//*[@id="content"]/input')
abs_path = os.path.abspath("Random.mp4")   #"random" this keep changing every hour
file_input.send_keys(abs_path)

I need something like

abs_path = os.path.abspath("*.mp4")    #any random filename *.mp4 needs to be uploaded . because it changes everytime. only one video file .mp4 exist at any point of time. 

if i give as *.mp4 then python script fails.

Need suggestion to change the script logic.

  • If you need to use `*.mp4` in your code, then try getting a result via **glob**. See my [example here](https://stackoverflow.com/a/74969466/2057709). PS: Your question has _"Every hour... A batch job creates a video with current date and writes to current working directory"_ so does that that mean you get 24 MP4 files stored in directory for every day? Or is it just one MP4 file always being stored (where the previous hour's MP4 is auto-deleted by batch job)? No need to reply these points, you have a solution now but mention such details in future questions for a possible answer. – VC.One Jan 26 '23 at 15:47

1 Answers1

1

I have solved the problem by scanning the working directory for any files with extension .mp4.
This way it finds the MP4 file and I can extract its (changed) current file name:

video_extensions = ['.mp4', '.avi', '.mkv']

current_directory = os.getcwd()

for root, dirs, files in os.walk(current_directory):
    for file in files:
        if any(file.endswith(ext) for ext in video_extensions):
            file_path = os.path.join(root, file)
            file_input = browser.find_element(By.XPATH, '//*[@id="content"]/input')
            file_input.send_keys(os.path.abspath(file_path))
VC.One
  • 14,790
  • 4
  • 25
  • 57