File sharing problem with Python inside Excel - python

I have python 2.6 script that creates a bunch of csv files on windows.
This script can be run stand alone or inside Excel via VBA shell command.
There are no problems when runs as stand alone, followed by VBA script.
When I run script inside Excel with a shell call. I have file sharing problems.
The Script creates runs, close files
fw = open(fn, "wb")
fw.write(....)
fw.close()
at the end of script I have:
os._exit(1)
Then Excel VBA does its stuff with the files. This gives error messages.
The error msg:
"FILE Now Available"
....is now avaiable for editing.
Choose read-write to open it for editing.
The script is multitheaded....

You may want to create a delay after the shell call (doEvents or sleep(100) might work) to allow the OS to properly close the file and remove all pointers to the file.

Related

Set Python script as default for opening file type

I am trying to set my Python script as default program to open a file (e.g. open every .txt file with my program when I double click on it).
I already tried this:
from sys import argv
# write the arguments to a file for debugging purposes
with open("output.txt", "w+") as f:
f.write(repr(argv))
I converted the script into a .exe with pyinstaller, otherwise Windows won't let me use it to open files.
In the command prompt, it works: typing main.exe some args indeed yields an output.txt file, with inside it ["C:\...\main.exe", "some", "args"].
I was hoping that by opening a .txt file with this script (in File Explorer > right click on file > open with > more apps > check "always use this app" and selecting the executable), it would be the same as running main.exe C:\...\that_file_that_i_just_clicked.txt in the command prompt, from which I could then use the file path to open it in my program. However, this does not happen. In fact, main.exe never even gets executed (because it doesn't even create a new output.txt).
How can I link a pyinstaller-generated executable to always open a filetype, and how do I then know the path of the opened file in Python?
The thing that I was doing wrong, was creating output.txt using a relative file path. Since the script was converted into an .exe (which basically wraps the interpreter and the script into a single file), the relative file path stopped working.
Using an absolute file path fixed my issue (as pointed out by Eryk Sun).

Unable to read text file when program is not started by the user

A simple program to read a text file then print it:
file=open('C:\\test_files\\data.txt', 'r')
read=str(file.read())
file.close
print(read)
input('')
This works fine when I run it (double clicking the program),
however when the program is started using a batch program
start C:\test_files\python_program.py
it starts then instantly closes.
.py files aren't executable, so unless you've associated the .py file extension with your Python interpreter, you can't call start on a .py file.
You also aren't providing a window title.
Try this command:
start "this is a python window" python c:\test_files\python_program.py

Having an issue running my python program in VS Code using open()

I am writing a Python program which reads data from a .txt file and writes to it. I am using VS Code as my editor and I am coming across an error where it doesn't recognize infile = open("poem.txt", "r") and tells me there is no such file or directory poem.txt. I can assure you it does exist and it is in the same folder. I can open the same program within IDLE and it runs fine. Is there a way to correct this error for VS Code without trying to hard code in absolute path for the file?
This code is not hardcoded, but will change with the directory the program is run from:
Join constructs file paths from arguments, and getcwd gets the current working directory.
import os
infile = open(os.join(os.getcwd(), "poem.txt"), "r")

Run .py when .txt is opened?

This feels like a simple question, but I don't know if there's a simple answer. I basically just need to make a .py file run when I open up a .txt. I don't really want a loop to check and then run it, preferably i'd like to bundle the txt and the py so all the user sees is a .txt file. Thanks!
EDIT: More Specifically, is there some type of program that can bundle the txt and py so when they double click the txt it runs the py as well as opening the txt file?
This is not as simple as it seems to be. For running a script when a file is being opened you somehow need to recognize the open file event, and I don't know if this would be possible, especially in Windows.
If you want to do something when the file is being modified (saved), you could give the watchdog package a try.
If this does not help, and you know what program is being used for opening the file, you could do it manually: Write a script which first does whatever you need to have done, then opens the text file using subprocess.call.

OSX: Creating an automator workflow with Python makes the workflow invalid

I'm making a chat client for OSX, and I wanted it to be able to run as both a .app (for my less technologically inclined users) and as a .py file. I made a workflow app that contained two .py files (an auto-updater and the client itself), run by a python script in the .wflow file. This worked well. However, I couldn't update the updater or workflow script, and the icon was the Python rocket instead of the icon I had chosen. Then, I combined the client .py file with the updater .py file. This still worked, and now I could update the updater. I still couldn't update the python script in the workflow, though, and the icon was still wrong. So, I modified the updater to open the .wflow file, split it into a list (based on python comments in the workflow's python script, such as "#Start") of the stuff before the script, the script's modification time, and the stuff after the script. If the modification time isn't the same as the modification time of the remote file (the one that the updater updates from), then the script downloads the remote .py file, replaces characters (<, >, &) that .wflow files replace ('<' -> "<"), and opens document.wflow with the "w" (write/replace) flag. Then, the stuff that was before the old script, the downloaded script, and the stuff that was after the old script (using file.write(''.join(list))) are all put into document.wflow. This should work, but OSX no longer sees document as an automator file.
As you can see, OSX thinks that the old file is a Workflow, while the new file is a "Microsoft Excel 97-2004 workbook". The IMClient.app (the application that contains document.wflow) gives this message when I try to run it: "The document "IMClient" could not be opened because it is damaged or incomplete." Does anyone know how to fix this?
I'm using python 2.7 and OSX 10.7. The updater is downloading files via FTP.
If clarification is necessary, just ask. I'll post the working and nonworking files if anyone wants them.
EDIT: the file command gives "document.wflow: XML document text" for both the old and new file.

Categories

Resources