Calling and running other python files from a python file - python

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...

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

Using Osmconvert with Python

I want to use osmconvert to parse down the size of my diff files for just the area I'm interested in because osmconvert is way faster than osm2pgsql, which loads the data.
When I call the command using os.system() like such:
cmd = r"""c:\temp\osmconvert.exe 770.osc.gz -b=1,1,3,3 -o=extract.o5m"""
os.system(cmd)
I get osmconvert error: cannot open file
When I run the same exact command from my command prompt in Windows 7, it runs fine. What is python doing to prevent this function from running? The 770.osc.gz file lives in the same directory as osmconvert.exe and the output extract.05m should populate in the same directory as the osmconvert.exe exists.
If I put the command in a batch file, it works, but I want to use python to download the file from the server so I can automate the updates of the database.
Thank you
The 770.osc.gz file lives in the same directory as osmconvert.exe and the output extract.05m should populate in the same directory as the osmconvert.exe exists.
That's not what your code is saying. The code says "execute osmconvert.exe from inside c:\temp\ but read 770.osc.gz and write extract.o5m from the current working directory".
If you want everything to run inside c:\temp\ then you either have change to this directory before executing osmconvert or you have to preprend the path to every file you are passing to osmconvert.
Try this call instead:
cmd = r"""c:\temp\osmconvert.exe c:\temp\770.osc.gz -b=1,1,3,3 -o=c:\temp\extract.o5m"""

Python: system command

I spend a few hours writing a little script.
Basically what it does is create a new text file and fills it up with whatever.
I zip the text file --using zipfile-- and here's where my problem lies.
I want to run the Windows system command:
copy /b "imgFile.jpg" + "zipFile.zip" newImage.jpg
To merge the image "imgFile.jpg" and the zip "zipFile.zip".
So:
os.system("copy /b \"imgFile.jpg\" + \"zipFile.zip\" newImage.jpg")
When I run my script, it all seems to go fine.
But when it's done and I try to extract the 'newImage.jpg' file, it gives me:
The archive is either in unknown format or damaged
This ONLY happens when I run the system command within the script.
It works fine when I use the shell. It even works if I use a separate script.
I've double checked my zip file. Everything is in good shape.
Is there something I'm doing wrong? Something I'm not seeing?
Have you tried using shutil?
import shutil
shutil.copy(src, dst)
There may be a problem with the way Python is passing the arguments to the shell command. Try using subprocess.call. This method takes arguments as an array and passes them that way to the command:
import subprocess
subprocess.call(["copy", "/b", '"imgFile.jpg" + "zipFile.zip"', "newImage.jpg"])

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