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.
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))
Related
I would like to save a CSV file in a specific folder, but I can't find anywhere how to do it...
this is the code
# Writing on a CSV FILE
fileToWrite = open(f"{userfinder}-{month}-{year}.csv', "a")
fileToWrite.write('Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description\n')
fileToWrite.write(f'{string1}{tagesinfo2},{soup_datum},{soup_dienstbegin},{soup_datum},{soup_dienstende},,Kommentar: {soup_kommentar} Schiff: {b} Funktion: {soup_funktion} Schichtdauer: {soup_schichtdauer} Bezahlte Zeit: {soup_bezahltezeit} Mannschaft: {crew_list2}\n')
fileToWrite.close()
print(f'Datum: {soup_datum} Dienst: {string1}{tagesinfo2} --> Mannschaft: {crew_list2} --> OK')
You just have to change the working directory with os.chdir(path):
import os
path = '/Users/user/folder/example sub folder'
os.chdir(path)
#your code here
or, as mentioned in the comments, you can use:
myfolder = "c:/foo/bar/"
fileToWrite = open(f"{myfolder}/{userfinder}-{month}-{year}.csv", "a")
#in this case the path is "{myfolder}/{userfinder}-{month}-{year}"
This option includes the path when opening (only affects the one file) whereas os.chdir() changes the directory for everything (what I use personally for all of my projects, which are small).
If you don't want to change your folder for all files created and read, use the second option; but when you want a python file to affect every file in a distant location I would use os.chdir().
I'm getting a
No such file or directory: 'SnP1000_TICKR.csv' error but all my files are in the following folder:
and I'm calling the file here
which is running on this piece of code:
def finVizEngine(input,output):
import chromedriver_autoinstaller
chromedriver_autoinstaller.install() # Check if the current version of chromedriver exists
# and if it doesn't exist, download it automatically,
# then add chromedriver to path
driver = webdriver.Chrome()
ipo_df = pd.DataFrame({})
openFinViz()
with open(input, 'r') as IPO_List:
csv_reader = reader(IPO_List)
This was running before, but then I uploaded files to Github and started running the files from vscode instead of pycharm and started to get a load of errors, but honestly don't understand what is wrong. Any help would be amazing,
Best Joao
First check in which folder it runs code
import os
print( os.getcwd() )
cwd means Current Working Directory.
If it runs in different folder then you have script then it also search csv in different folder.
The simplest method is to use "/full/path/to/SnP1000_TICKR.csv".
But more useful method is to get path to folder with script - like this
BASE = os.path.abspath(os.path.dirname(__file__))
and use it to create full path to file csv
input_full_path = os.path.join(BASE, "SnP1000_TICKR.csv")
output_full_path = os.path.join(BASE, "SnP1000_DATA.csv")
finVizEngine(input_full_path, output_full_path)
BTW:
If you will keep csv in subfolder data then you will need
input_full_path = os.path.join(BASE, "data", SnP1000_TICKR.csv")
I am using YouTube-dl with Python and Flask to download youtube videos and return them using the send_file function.
When running locally I have been using to get the file path:
username = getpass.getuser()
directories = os.listdir(rf'C:\\Users\\{username}')
I then download the video with YouTube-dl:
youtube_dl.YoutubeDL().download([link])
I then search the directory for the file based on the video code:
files = [file for file in directories]
code = link.split('v=')[1]
for file in files:
if file.endswith('.mp4') is True:
try:
code_section = file.split('-')[1].split('.mp4')[0]
if code in code_section:
return send_file(rf'C:\\Users\\{username}\\{file}')
except:
continue
Finally, I return the file:
return send_file(rf'C:\\Users\\{username}\\{file}')
to find the location of the downloaded file, but, on Heroku, this doesn't work - simply the directory doesn't exist. How would I find where the file is downloaded? Is there a function I can call? Or is there a set path it would go to?
Or alternatively, is there a way to set the download location with YouTube-dl?
Since heroku is running Linux and not windows, you could attempt to download your files to your current working directory and then just send it from there.
The main tweak would be setting up some options in your YoutubeDL app:
import os
opts = {
"outtmpl": f"{os.getcwd()}/(title)s.(ext)s"
}
youtube_dl.YoutubeDL(opts).download([link])
That will download the file to your current working directory.
Then you can just upload it from your working directory using return send_file(file).
I wan to make a instagram photo downloader using python , i will file all the image url for me and then download it to the folder that i specified .
I manage to create a folder also getting the url , but the image doesnt seem to be like downloading to the folder.
Below is the code
def download_image(self, src, image_filename, folder):
"""
Creates a folder named after a user to to store the image, then downloads the image to the folder.
"""
folder_path = 'C:/{}'.format(folder)
#folder_path = 'C:\\.{}'.format(folder)
if not os.path.exists(folder_path):
os.mkdir(folder_path)
print("folder not create")
img_filename = 'image_{}.jpg'.format(image_filename)
print(str(src)," File Name",str(image_filename)," Folder ",str(folder))
urllib.request.urlretrieve(src, '{}/{}'.format(folder, img_filename))
The output is this
# image url
https://instagram.fkul8-1.fna.fbcdn.net/v/t51.2885-15/sh0.08/e35/c120.0.720.720a/s640x640/19534375_479423379059793_7995041966070956032_n.jpg?_nc_ht=instagram.fkul8-1.fna.fbcdn.net&_nc_cat=109&oh=7c8e2f8816f17627424bf38d0267f39b&oe=5E7DFCA1
# file name is the image name and folder name is kwting3
File Name kwt1 Folder kwting3
Have you tried setting the download file path in your driver initialization? This is usually the accepted method for downloading files to a specific folder.
options = webdriver.ChromeOptions()
options.add_argument("download.default_directory=C:/Downloads") # specify some other file path here
driver = webdriver.Chrome(chrome_options=options)
You will add this code at the very beginning, where your WebDriver object gets initialized.
I have to download files from the web over several requests. The downloaded files for each request have to be put in a folder with the same name as the request number.
For example:
My script is now running to download files for request number 87665. So all the downloaded files are to be put in the destination folder Current Download\Attachment87665. So how do I do that?
What I have tried so far:
my_dir = "D:\Current Download"
my_dir = os.path.expanduser(my_dir)
if not os.path.exists(my_dir):
os.makedirs(my_dir)
But it doesn't meet my original requirement. Any idea how to achieve this?
Just create a path beforehand, via os.path.join:
request_number = 82673
# base dir
_dir = "D:\Current Download"
# create dynamic name, like "D:\Current Download\Attachment82673"
_dir = os.path.join(_dir, 'Attachment%s' % request_number)
# create 'dynamic' dir, if it does not exist
if not os.path.exists(_dir):
os.makedirs(_dir)