I am working on a bot that downloads a file using python and selenium webdriver. I have successfully been able to design the bot and it is working perfectly. The issue is
I want to target this file that was just downloaded. This program is going to be used by many people who may have set different directory for downloads. How do I use the OS module to achieve this?
Once the data is downloaded from the site, the default name is transactions; my users may have other files with the same name and my program might copy a wrong file. The file is in an excel format.
How do I target this file alone and not any other file even if they have the same name or is there a way I can change the directory my bot save any file it downloads so I don't need to copy to the directory where I want it for calculations?
consider use of shutil
newfilename = os.path.join(os.path.abspath('foopath'), 'foofile') shutil.copyfile('transactions', newfilename)
Reference other options at
copy-a-file-from-one-location-to-another-in-python
Related
I am using Robocorp, an RPA platform.
In my bot, I have to click a link that automatically downloads a file. (the file name is generated randomly by the site)
I then need to rename the file and move it to a specific directory.
I have two questions on the best way to do this:
Should I simply change the Chrome settings on the RPA.Browser.Selenium library to ask for the download location upon downloading. (Note that I have not been able to get this to work in Robocorp)
Or should I wait for a new file to appear in the bot's "Downloads" folder and then manage that file from there?
Above is an image of one of my many attempts of editing the 'options' argument of the Open Available Browser task to ask me where to save the file prior to downloading. I noticed there is also a 'download' argument but I do not believe it is the right one to edit based on some research.
https://robocorp.com/docs/libraries/rpa-framework/rpa-browser/keywords#open-available-browser
Here is the docs page for the "Open Available Browser' task in Selenium that shows all of its arguments.
Is there a more reliable way to do this?
This is a bit tricky scenario. First, I will start by stating I do not have any experience in Robocorp tool. But this is a generic problem in test automation and RPA.
There are few things to consider while waiting for a file to download including;
Is the file fully downloaded?
What is the max time I need to allow for download?
What if there are pervious files in the folder with similar name?
So to overcome this I would take the following approach. (tested and working for more than 4+ prod level automation frameworks with >20k scenarios)
Maintain a unique "Downloads" folder for each execution. Do this by creating a new folder with unique name and setting that as the downloads folder at the beginning of the run.
In a loop, check the download file size continuously and wait for the file size to NOT increase to check the file is fully downloaded.
??? profit ???
I am wondering if there is an easy way to access 'parallel' directories (See photo for what I am talking about... I don't know what else to call them, please correct me if they are called something else!) from a Python file without having to input the string path.
The basic structure I intend to use is shown in the picture. The structure will be used across different computers, so I need to avoid just typing in "C:\stuff_to_get_there\parent_directory\data\file.txt" because "C:\stuff_to_get_there" will not be the same on different computers.
I want to store the .py files in their own directory, then access the data files in data directory, and save figures to figures directory. I was thinking of trying os module but not sure if that's the correct way to go.
parent directory
scripts
.py files
figures
save files here
data
.txt files stored here
Thanks for any help!
Ok so... I'm trying to make an discord bot as some of you know, and I want to organize the logs while they are created so... for example... when I open the bot file to run (draco.py) it creates an file called 1_logs and when I close it keeps saved and when I open the file to the bot to run it makes the file 2_logs
Is that possible? Ive tried creating another file that counts what log number is with file.read() and file.write(), but I didnt had any success...
Is that possible?
If you are keeping the log files in a folder, you can easily search through your folder using either os, sys or glob modules. Then you can sort them.
But still what #ForceBru suggested is easier.
For example:
glob.glob('*_logs') # will give you all the log files you have.
A python package that I'm using has data stored under a single file with a .pkz extension. How would I unzip (?) this file to view the format of data within?
Looks like what you are referencing is just a one-off file format used in sample data in scikit-learn. The .pkz is just a compressed version of a Python pickle file which usually has the extension .pkl.
Specifically you can see this in one of their sample files here along with the fact they are using the zlib_codec. To open it, you can go in reverse or try uncompressing from the command line.
Before attempting to open an PKZ file, you'll need to determine what kind of file you are dealing with and whether it is even possible to open or view the file format.
Files which are given the .PKZ extension are known as Winoncd Images Mask files, however other file types may also use this extension. If you are aware of any additional file formats that use the PKZ extension, please let us know.
How to open a PKZ file:
The best way to open an PKZ file is to simply double-click it and let the default assoisated application open the file. If you are unable to open the file this way, it may be because you do not have the correct application associated with the extension to view or edit the PKZ file.
If you can do it, great, you have a program installed that can do it, lets say that program is called pkzexecutor.exe, with python, you just have to do:
import subprocess
import os
path_to_notepad = 'C:\\Windows\\System32\\pkzexecutor.exe'
path_to_file = 'C:\\Users\\Desktop\\yourfile.pkz'
subprocess.call([path_to_notepad, path_to_file])
From the source code for fetch_olivetti_faces, the file appears to be downloaded from http://cs.nyu.edu/~roweis/data/ and originally has a .mat file extension, meaning it is actually a MATLAB file. If you have access to MATLAB or another program which can read those files, try opening it from there with the original file extension and see what that gives you.
(If you want to try opening this file in Python itself, then perhaps give this question a look: Read .mat files in Python )
This might be a general programming question but since I am doing it from within an Add-In therefore asking here at GIS forum. I have a project folder with sub-folders containing several files on my hard disk which I read from within my Python Add-In, its hard coded e.g.:
dem = r'C:/project/raster/dem'
and Add-In is in
r'C:/project/Add-In'
folder. I tried doing '../raster/dem' to define path of input raster layer but it failed to read. Please suggest how can I make it generic so that if I move project folder to D drive then Add-In would still be able to read data.
You can reference materials that are located within the add-in, everything that's included within the 'Install' directory will be copied into the appropriate location within AssemblyCache. You can then reference this by doing something like:
local_path = os.path.abspath(os.path.basename(__file__))
raster_path = os.path.join(local_path, 'rasters')
And from there, treat raster_path as a nomral path.
You could read in a configuration file stored under the user's profile. Because ArcGIS add-ins overwrite themselves every time the host application is opened, you don't want to store user-specific configuration information inside the add-in itself.
The configuration file can be in any format you want (e.g. XML, plain text), but the ConfigParser class makes reading and writing to an INI-like file format easy.