How to start external program and pass in CSV file? - python

I am trying to start a program and write a saved CSV file to it.
I have the following code to start the program, that works fine:
os.startfile('C:\Program Files\Files\CSV_Reader')
But I'm not sure how to open the CSV File. I tried a few other options like:
os.system('start CSV_Reader.exe "{0}C:\Program Files\Files\card_kingdom.csv"'.format(sys.path[0], ))
But I get the following error message:
C:\Users\JJ\AppData\Local\Programs\Python\Python36\python.exe C:/Users/JJ/PycharmProjects/Buylist/CK_file_test
The system cannot find the file CSV_Reader.exe.
I know that the directory is right, because when I call the os.startfile function it opens the program.
Your help would be much appreciated!

You can use popen like it:
import subprocess
subprocess.Popen(["C:\Program Files\Files\CSV_Reader.exe" , "C:\Program Files\Files\card_kingdom.csv"])

Related

How to call clang-format from Python script

I have a Python tool that generates C++ files.
In order to test the tool, I have one test that compares the generated file with an expected output file.
diff = difflib.unified_diff(expectedFile.readlines(), file.readlines(), expectedFilename, filename)
The problem is that I'm getting some differences due to the format.
I can run clang-format on the expected output file.
What I'm still trying to do is to run clang-format on the generated files, just before the difflib.unified_diff is called.
Can anyone help me on how I can run clang-format in Python on a file ?
Thank you very much!
You can use the call command that is supplied by Python to call an external command. For example, you can write a script like:
#!/usr/bin/python
import sys
from subprocess import call
lc = ["clang-format","test.c"] # replace 'test.c' with the your filename.
retcode=call(lc)
sys.exit(retcode);

how to modify txt file properties with python

I am trying to make a python program that creates and writes in a txt file.
the program works, but I want it to cross the "hidden" thing in the txt file's properties, so that the txt can't be seen without using the python program I made. I have no clues how to do that, please understand I am a beginner in python.
I'm not 100% sure but I don't think you can do this in Python. I'd suggest finding a simple Visual Basic script and running it from your Python file.
Assuming you mean the file-properties, where you can set a file as "hidden". Like in Windows as seen in screenshot below:
Use operating-system's command-line from Python
For example in Windows command-line attrib +h Secret_File.txt to hide a file in CMD.
import subprocess
subprocess.run(["attrib", "+h", "Secret_File.txt"])
See also:
How to execute a program or call a system command?
Directly call OS functions (Windows)
import ctypes
path = "my_hidden_file.txt"
ctypes.windll.kernel32.SetFileAttributesW(path, 2)
See also:
Hide Folders/ File with Python
Rename the file (Linux)
import os
filename = "my_hidden_file.txt"
os.rename(filename, '.'+filename) # the prefix dot means hidden in Linux
See also:
How to rename a file using Python

Calling and running other python files from a python file

so i checked several other links with similar titles but, It couldn't solve my specific question. I'm trying to run a python file in notepad++ which is not a problem to me however, this file takes in a few things in order for it to compile. This is how I successfully run it in the command prompt.
python upload.py --file= "video path" --title= "title" --description= "testing"
My question is, how would i set these attributes in a different python file and then just call that file instead?
here is my code that i have in my new file
Thanks
enter image description here
enter image description here
You can use the subprocess module to do this. Following the example from the docs and the code you've listed:
import subprocess
result = subprocess.check_output('python upload.py --file="video path" --title="title" --description="testing"')
result will store any output from your command.
Note: if you're running in a windows environent, not linux, change the /usr/bin/python to python.
Maybe you can use subprocess to call your specific command.
In a separate file in the same folder, you can put a file like this:
import subprocess
subprocess.call("python upload.py --file= \"video path\" --title= \"title\" --description= \"testing\"")
And then you just call that file, and that's it...

Opening python file with an input

Here is the code I have so far:
How can I make this program open another python file, using this method or similar (you have to open it from a variable)?
You can use exec function, for to execute an external script,
file = "test.py"
exec(open(file).read())
you get,
File Opened!

How do I run a python file in pylab, which executes another python file?

I want to create a startup configuration that runs a file that I request. So far, my configuration file is as follows:
path1=input('What folder would you like to open?')
os.chdir('C:\\Users\\Owner\\Documents\\Spring 2013\\CSCI_278\\'+path1)
doc=input('What file would you like to open and run?')
open(doc)
execfile(doc)
but the execfile doesn't work for some reason, and I end having to use %run in pylab anyway. Is there a way around this?
Does using raw_input instead of input solve your problem?
Not sure you need the open(doc) line.
The code below works on my machine:
doc = raw_input('What file would you like to open and run?')
execfile(doc)
Note that you can also use the line below instead, if you do not want to type the ".py" each time
doc = "%s.py" % raw_input('What file would you like to open and run?')

Categories