pythonpytube

Automation Script throwing "No such file or directory: '.DS_Store'" error


So I tried my best and I feel like I am at the point where I need some assistance from others to complete a script. Below, I will explain the cause that I am wanting to use it for and all that.

Here is what I need: "I need the ability to paste a YouTube URL into a input, have the script fetch the highest quality video (Pytube only fetched 720p, I don't know why it doesn't fetch 1080p), and then download the video into a folder on my desktop called "Videos".

Since Pytube only allows up to 720p and not 1080p or 4K, I am using selenium in this format.

  1. Input YouTube URL in CMD.
  2. Trigger script to open "Vinteo.app".
  3. Input YouTube into Vinteo.app search input.
  4. Click on "Video" to download since it auto selects 1080p in the modal.
  5. Download the video into my "Videos" folder.

However, I am having some issues and I am getting this error:

Traceback (most recent call last):
  File "/Users/ss/Desktop/Videos/import-videos.py", line 36, in <module>
    download_youtube_video(url)
  File "/Users/ss/Desktop/Videos/import-videos.py", line 30, in download_youtube_video
    os.rename(filename, destination)
FileNotFoundError: [Errno 2] No such file or directory: '.DS_Store' -> 'C:/Users/ss/Desktop/Videos/.DS_Store'

Here is my codebase:

import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

def download_youtube_video(url):
    driver = webdriver.Chrome()
    driver.get("https://vinteo.app")
    
    # Wait for search box to be present and input url
    search_box = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#search"))
    )
    search_box.send_keys(url)

    # Wait for download button to be clickable and click it  
    download_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#download-video-default"))
    )
    download_button.click()

    # Wait for download to start
    time.sleep(5)
    
    # Move file to Videos folder
    filename = os.listdir(os.getcwd())[0]
    destination = os.path.join("C:/Users/ss/Desktop/Videos", filename)
    os.rename(filename, destination)
    
    driver.quit()

if __name__ == "__main__":
    url = input("Enter YouTube URL: ")
    download_youtube_video(url)

I believe I need some help properly targeting the elements also and actually triggering a download. All help is appreciated!

I expect the YouTube video to download, but I am running into a "FileNotFoundError: [Errno 2] No such file or directory: '.DS_Store' -> 'C:/Users/ss/Desktop/Videos/.DS_Store'" error.


Solution

  • FileNotFoundError: [Errno 2] No such file or directory: '.DS_Store' -> 'C:/Users/ss/Desktop/Videos/.DS_Store'
    

    The error is telling you that the C:/Users/ss/Desktop/Videos directory does not exist.

    os.rename() will not automatically create the destination directory. You must ensure that it exists.