'How to get Python to open a file that I have just created?
So I am outputting a CSV using Python and Pandas, based on a certain criteria I have. Once building the the CSV file, I'd like to run a command that has Python open the CSV file in Excel.
Would something like this people possible?
Solution 1:[1]
If you know the location of your Excel executable:
import subprocess
subprocess.call(("path/to/Excel.exe", "path/to/your.csv"))
If you don't know the location (but Excel is set as default app for .csv files) it depends on the OS you're using, but since you want Excel I assume it's either Windows or OSX:
import os
import subprocess
if os.name == "nt":
os.startfile("path/to/your.csv")
else:
subprocess.call(("open", "path/to/your.csv"))
Solution 2:[2]
This solved it for the OS to find the associated application automatically (Excel or other) in my case on Windows, starting with the answer by zwer.
import subprocess as sp
filename = 'some.csv'
sp.Popen(filename, shell=True)
Resorted to subprocess because os.system(filename) decided to start to hang.
shell=True resolves the problem of OSError: [WinError 193] %1 is not a valid Win32 application
.Popen() instead of .call() was necessary using that lone filename argument.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zwer |
| Solution 2 | gseattle |
