Use one file on another - Python - Windows - python

I have a directory of .xml files, and one exe file.
I would like to write a python script which will use those .xml files (one at a time) on the .exe file emulating dragging and dropping. It cannot however actually control the mouse cursor.
I've looked for resources on this but cannot find anything.
Any hints in the right direction would be very helpful, if it is even do-able.
Thank you for your time.

Forget about controlling the cursor: Dragging and dropping on Windows simply calls the program you drop things on, with the full path of the dropped files as arguments. So if you want to run myprogram.exe on all files in its folder, do something like this:
from glob import glob
import subprocess
for filename in glob("./*.xml"):
subprocess.call(["myprogram.exe", filename])
You were not entirely clear on what you wanted to do, so you may have to make some adjustments, but it should be along such lines. The call to glob is to ensure that only xml files get run.

Related

Is there a method for displaying a folder onscreen?

I am creating a program in Python that requires the user to place images into an Input folder, and then take images out of an Output folder. As this will become an application, the Input and Output folders will be very difficult to navigate to, being buried in the app's contents.
I am looking for a way to open folders onscreen so that a user can add or remove their own files from these folders, without knowing the exact location of the folders they are interacting with.
I am thoroughly stumped on this problem, and I appreciate all of your time.
Thank you very much.
*Edit: I am working on MacOS
If the program is intended for use on windows, it seems like you could use the solution here, where you could open explorer as a sub process and then open the path to the file.
For example
import subprocess
subprocess.Popen(r'explorer /select, FilePath')
You could also use os, and os.startfile(FilePath).

Can I make python read a pdf without giving the path but by having the file open on the computer instead?

I am very new to python and it seems that every tutorial mentions the need for a path when opening and reading a pdf in python.
The pdf that I want to read can be located in different folders and have different names and I would like to avoid relying on user input. Instead I was hoping that I could open the pdf in Acrobat Reader on the computer and code python to read the pdf file that is currently open. Is this possible?
One solution is to make use of the console arguments that can be accessed via the 'sys' module, the first argument is the path to the python executable, and the rest are arguments passed by the user like this:
python someScript.py arga argb
Then you can access them like this via the argv array:
import sys
for arg in sys.argv:
print(arg)
In this example it would print:
path/to/someScript.py
arga
argb
You could make it so you take the 1st user argument as path like this:
python someScript.py some/path/to/some/file.txt
And then you can get that path using
import sys
path = sys.argv[1]
If you dislike the user having to type the path in the terminal when launching the python script, or prefer launching it by double clicking the py file, you can just drag and drop the file you want the path of onto the python file and then it will work just fine.
I do realise that its not exactly what you asked, but finding an open file in some process on the pc is way harder and more unrealistic, if its even possible.
Hence i am proposing this alternative solution
It's possible but paths are so much easier, lol. checkout pyautogui. After you configure the script which navigates to the window you're looking for you're going to need to take a screenshot and use optical content recognition, checkout pytesseract

How do I use subprocess to delete a file in a zip folder using Python

[Python 2.6.5] Simple question but can't seem to find any documentation or previous questions answering my problem. I'm trying to delete 2 files within a ZIP Folder (Its 2 Folders deep in the ZIP Folder if that matters). I want to use subprocess to call 7Zip to delete the files but have no clue how to even start that process. As of now I can only open the application. Ideally, I'd like to run the python script, have the file names hard coded into the script, and have 7zip just automatically delete those files without the application ever opening if possible.
What I have so far:
import subprocess, os
rawLocation = 'SomeLocation\7-ZipPortable.exe'
subprocess.Popen([rawLocation])
This successfully opens the application, but I'd like to run and have it automatically delete files without even opening up if possible.
Note: Thanks for the help everyone. The customer's requirements ended up changing so I don't need to use this logic anymore.
You need to use the command line version of 7zip. And then according to this documentation, you should be able to delete files like so.
7z d archive.zip deleteme.ext
Note: for anyone who might think that Python's zipfile module would be a good solution, I looked through the module, and it doesn't look like it supports deleting files from an archive.
something like this :
from subprocess import call
delete_me = "note.txt"
zip_file = "archive.zip"
try:
call(['7z d', zip_file, delete_me])
except OSError:
# handle errors here
pass

Python watchdog event not returning entire src_path

I'm using python watchdog to keep track of what files have been changed locally. Because I'm not keeping track of an entire directory but specific files, I'm using watchdog's event.src_path to check if the changed file is the one I'm looking for.
I'm using the FileSystemEventHandler and on_modified, printing the src_path. However, when I edit a file that should have the path /home/user/project/test in gedit, I get two paths, one that looks like /home/user/project/.goutputstream-XXXXXX and one that looks something like this: home/user/project/. I never get the path I'm expecting. I thought there may have been something wrong with watchdog or my own code, but I tested the exact same process in vi, nano, my IDE (PyCharm), Sublime Text, Atom...and they all gave me the src_path I'm expecting.
I'm wondering if there is a workaround for gedit, since gedit is the default text editor for many Linux distributions...Thanks in advance.
From the Watchdog GitHub readme:
Vim does not modify files unless directed to do so. It creates backup files
and then swaps them in to replace the files you are editing on the
disk. This means that if you use Vim to edit your files, the
on-modified events for those files will not be triggered by watchdog.
You may need to configure Vim to appropriately to disable this
feature.
As the quote says your issue is due to how these text editors modify files. Basically rather than directly modifying the file, then create "buffer" files that store the edited data. In your case this file is probably .goutputstream-XXXXXX. When you hit save your original file is deleted and a the buffer file is renamed into its place. So your second path is probably the result of the original file being deleted. Sometimes these files serve as backups instead, but still cause similar issues..
By far the easiest method to solve this issue is to disable the weird way of saving in your chosen text editor. In gedit this is done by unchecking the "Create a backup copy of file before saving" option within preferences. This will stop those backup files from being created and simplify life for watchdog.
Image and preference info shamelessly stolen from this AskUbuntu question
For more information (and specific information for solving vim/vi) see this issue on the watchdog GitHub.
Basically for Vim you need to run these commands to disable the backup/swapping in feature:
:set nobackup
:set nowritebackup
You can add them to your .vimrc to automate the task

How to list (certain) files in a directory dynamically in Python?

I am trying to list (let's say) PDF files in a directory (to a GUI). I know I can manage that with in different ways (like below) but I also want the list is to be current and dynamic. For instance, if someone deletes a PDF file from or adds a PDF file to the directory, I want the application to delete or add the file name to the list instantly, without any refresh.
Do you know any ways to manage that?
Here is some of my code:
import os
pdffiles = [name for name in os.listdir('somedir')
if name.endswith('.pdf')]
for file in pdffiles:
print(file)
I am in a learning progress.
Your code looks good so far. If you want to do this programmatically (avoiding pynotify or signal handling), the simplest way to maintain a directory listing in your application is via polling the directory.
So, your code might look like:
import os
import time
## run indefinitely
while True:
## get the directory listing
pdffiles = [name for name in os.listdir('somedir')
if name.endswith('.pdf')]
## print the most recent listing
for file in pdffiles:
print(file)
## wait a little bit before getting the next listing
## if you want near instantaneous updates, make the sleep value small.
time.sleep(1)
You need to get notified by the OS that the directory and/or some files changed.
On Linux you can use pyinotify
On OSX you can do that with MACFSEvents
On Windows (.Net) you can try to interface with FileSystemWatcher, but I am not aware of any specific libraries for that.
And a platform independent solutions is watchdog
Hooking that into your program is non-trivial, but the examples of watchdog are clear and that means there is no need for polling.

Categories