Add file to "Recent Files" from my script - python

I've got a Python script for retrieving PDF documents from a remote system and storing them locally. I run it manually from a Linux command line (using Ubuntu).
Whenever I store the file locally I would like to somehow add it to Recent Files so that it shows in the file dialog without me having to navigate through the directory structure to it.
How is this done? Is it some dbus magic? I don't need a complete solution, just a pointer where to look because I have no idea...
Thanks!

Related

Batch file with remote drive python script

I was working on a python script that I want to run via the task manager. I created a batch file like this:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe" "\\insert_path_here\project_directory.py"
pause
When I have the project_directory.py as a path on my hard drive, it works fine with either the task manager or manually opening the batch file. When I put the .py file on a remote drive, the batch file still executes correctly when I manually open it. When the task manager tries runs the .bat file, I get a message asking which application I want to use to run the program:
What do?
Thanks!
Try using cmd.exe. on default you should find it here: c:\windows\system32\cmd.exe.
On the side note explore option of converting .py to .exe, you can start here: https://www.simplifiedpython.net/convert-python-to-exe-tutorial/
I feel like it would be better option for you.
It looks like it is trying to run the python.exe and then the project_directory.py file separately. Try putting those in as one command:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe \insert_path_here\project_directory.py"
You probably have *.py files already associated with Python on your local machine which is why it works there.

How to get a file from the files on my computer to JupyterLab?

I am very new to this and struggling with the basics. I have a csv file /home/emily/Downloads/Roger-Federer.csv The textbook says that I need to "extract the file and download it to my current directory" on JupyterLab (I am using Python). What does this mean? How do I do this? Thank you
Every running program, including JupyterLab, has a "working directory" which is where it thinks it is on your computer's file system. What exactly this directory is usually depends on how you launched it (e.g., when you run a program from terminal, its working directory is initially the folder your terminal was in when you ran the command, but it's possible for a program to change its own working directory later).
Your file path indicates you're on Linux, so I'd suggest opening a terminal in your JupyterLab and running pwd to have it print out its current directory. (You can also run !pwd in any open notebook if that's easier.) You should then copy your CSV file to that directory.
If you do that, then from your Python code, you can just open the file locally, like open('Roger-Federer.csv') or pandas.read_csv('Roger-Federer.csv'). You don't have to move the file to open it from Python, though, you can just give it the entire file path, like open('/home/emily/Downloads/Roger-Federer.csv'), and that'll work just fine too.

FileNotFoundError Python Script

I am trying to run a python script, .py in the windows command prompt.
I drag the script from my files into the command prompt window. I run it.
Then, the script presents a prompt for me to enter the file I would like to parse. I am supposed to enter a file like this: filename.txt
When I go to do this, I get the FileNotFoundError. They are both located in my downloads folder. Any ideas as to why? I've tried a couple of things but having no luck.
The script must be performed this way because this is a script many people will use and it is likely that they will receive it and download it and the file to parse to their computer so it'll be located in their downloads folder.
When I created the script I did it in Notepad++ and had a separate folder where I put the file and script and it worked fine this way. I am just ensuring it works from the perspective of someone who's downloading it off an email or website, etc.
Thanks !
Yeah that happened to me a lot.
To solve it just write the whole directory of the file.
If you are in windows
c:\user\username\Desktop\file.txt
If you are in linux or mac:
/home/username/Desktop/file.txt

Packaged Python program for Mac will not run when downloaded

I have Python program that I packaged with py2app so that the program query_agent.py became query_agent.app. I have a Java program that launches query_agent.app with the following command:
open ./query_agent.app --args abs_path1 abs_path2
, where abs_path1 and abs_path2 are string arguments.
I put everything in a folder and when I launch the JAR that eventually launches query_agent.app, everything runs perfectly as expected.
So I zip this folder so I can distribute it to others. Before sending, I put the zip on a flash drive and unzip it on another Mac computer to test it. Again, the program works as expected even when unzipped.
However, when I send the zip as a message attachment in an email, or upload the zip to a file hosting site like MediaFire, the program no longer works when unzipped. When I unzip the downloaded zip file on any Mac computer, right-click and open, I get the message:
query_agent Error
When I open the console, it shows the cause of the error:
The STATS_FILE_LOCATION points to a file that I am trying to write to. This file is located at query_agent.app/Contents/Resources/resources. As a reminder, this works fine if I use the version of the zip before it was uploaded.
It appears that the main error is shown in the last line as
OSError: [Errno 30] Read-only file system: 'resources/status.txt'
The first thing I notice is that it is finding my source files in the /private/var/folder/... location. I believe this a temp location, which may be why I am getting the "read-only file system" error. I never intended for anything to be read from or written to any location outside of my main applications folder.
I also get the message:
Detected missing constraints for <private>. It cannot be placed because there are not enough constraints to fully define the size and origin. Add the missing constraints, or set translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.
So my question is: Why doesn't my program work if I upload it, download it, unzip it, and then try to run it?
It works before the upload on any Mac computer I put it on, so the fact that it gets zipped is not the issue. I hope that these obscure error messages can provide some hints to why this is happening. Any help with solving this problem will be greatly appreciated.

Python correctly finding and reading windows application event logs

so my ultimate goal is to use python to read a specific application's windows event log when triggered by a file update.
Here is my problem, python I believe does not have access to the event logs stored in C:\Windows\System32\winevt\Logs. Whenever I try to read the files I get the following error:
WindowsError: [Error 2] The system cannot find the file specified
I tried every form of escaping, string split/join and using quotes on the file path and I always get the same error. I even cheaply used the os.system('dir "C:\Windows\System32..."') command in the python command prompt to list directories higher in the path for the log to verify access and I receive similar errors up to the C:\Windows\System32 directory, that one will list just fine.
Example:
C:\Windows\System32\winevt\Logs - File not found
C:\Windows\System32\winevt - File not found
C:\Windows\System32 - Lists files
I know these directories exist. So from here I figured I could use a bash script to copy the event log into a temp folder for python to read. I wrote a real simple bash script containing:
xcopy /Y "C:\Windows\System32\winevt\Logs\XXXXXXX" c:\Temp
(XXXXXXX) being the name of the log I want to copy for the python script.
The bash script runs fine on its own but when called by my python script it fails, refuses to copy the file because it can't find it. I have even tried using py2exe to create an exe to then run in administrator mode. This failed too. With similar errors of the file not found. I'm not sure I understand the permissions python uses to call the script and why the bash script cannot copy the file even when it can do it in a normal command prompt.
Any suggestions or flaws you can point out in my logic would be great. I am very stuck.
You are using a 32bits python on a 64bit install of windows.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64
You can use os.listdir("c:\windows\sysnative\winevt\logs") as a workaround to read from the real system32 dir from a 32bit python interpretter runing on a 64bit windows...
Sources:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
http://support.microsoft.com/kb/942589

Categories