So I have an .exe tool that needs to be executed in cmd that's used to convert some GIS data from one format to BAG format. The syntax looks like so:
C:\Caris\BDB\4.0\bin > carisbatch -r SurfacetoBAG [options] "input_file" "output_file"
I have a directory of about 40 files of GIS data, so I'd like to know how to automate the above cmd tool such that it will run through use all 40 files as "input_file". I've looked into using the subprocess() module wrapped in a for loop with python, but I am under the impression it can only be used for unix systems. Any ideas?
If you import os, you should be able to use
os.system('your command')
Regardless of platform (of course, the command string will vary between platforms)
you can use subprocess no problem on windows ...
example to follow
import subprocess
for file in my_files:
subprocess.Popen(["C:/Caris/BDB/4.0/bin/carisbatch.exe",'-r','SurfacetoBAGfile',file+".output"])
if you need to do it without the list format
for file in my_files:
subprocess.Popen('C:/Caris/BDB/4.0/bin/carisbatch.exe -r SurfacetoBAGfile "{0}" "{0}.output"'.format(file),shell=True)
Related
I have multiple configuration files and I want to read a different config file based on which option I type. For example if I type in the terminal
python test.py -60min
I want to read the python script to read the config file '/home/matt/config_60min.ini'
Similarly, python test.py -30min would read '/home/matt/config_30min.ini'
I'm not sure if this would be done using conditional logic within the script or with a simple option parser. Maybe there's a better way to go about it such as python test.py -f 60min
Thanks in advance.
You could do it like this:
import sys
config_path = '/home/matt/config_{}.ini'.format(sys.argv[1])
and run the script using:
python test.py 60min
If it gets more complicated than this, consider using the argparse library
I'm trying to figure out how to duplicate a single file for example: I have a file in "C:\ ..." and would like to duplicate this exact same file once. If also possible is there a way to use python to open specific documents?
thanks
I think you might be looking for copy2. This will copy the file contents and as much of the file metadata (permissions, ownership, etc) as it can on the platform. copystat has more notes on what can and can't be copied and how to find out on your platform.
just using system() from os module
os.system("cp resource_file target_file")
Using a module named shutil, the function copy2 can be called with the path of the source file and the corresponding destination directory you want to write to. For example,
import shutil
shutil.copy2('/src/test.txt','/dst/test_copy.txt')
you can copy a file via the command line on windows: open cmd.exe then type cd "C:\ ..." then type copy yourfile destination. more info here
you can make python do this for you: you will need the subprocess module which comes integrated into python so you dont have to donwload anything.
like this:
import subprocess
subprocess.run('copy yourfile destionation')
remember that for this to work your python script should be in the same folder as "yourfile", why? because i type "yourfile" as a relative path relative vs absolute path
subprocess works for python 3.3 and newer versions so another way to do it would be:
import os
os.system('copy youfile destionation')
to open specific documents with python take a quick look at here: https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python
I am using a python package Molbery, A tool for Molecular biologists, the usage is like
molbery -o output_file_path input_path
I am working with python CGI script and want to have the above command to execute from the that CGI script. and then the resultant output of the output file would be displayed in a webpage
One way is to do it as a systems call:
from subprocess import call
call(["some_command ", "your_args"])
... or ...
import os
os.system("some_command your_args")
However usually, you can use the module directly by importing it and using it's functions and modules. I don't seem to find any documentation for this so the first thing I'd do is to look into the source code itself. Especially the entry point (i.e., main function/module).
If I understand your question properly this should works for you
import os
os.system("molbery -o output_file_path input_path")
or this
from subprocess import call
call('molbery -o output_file_path input_path')
You can also see Calling an external command in Python
I am making use of Beyond Compare 3 to see the difference between two XML files. I am willing to make a small python script which on executing will open files ready to compare in Beyond Compare tool.
So far I tried invoking BC3 from command line syntax as below and it works:
BCompare.exe "c:\Ref-2.xml" "c:\Cop-2.xml"
but when I try to execute same syntax from python script as shown below, It throws error
from subprocess import check_output
check_output('BCompare.exe "c:\Ref-2.xml" "c:\Cop-2.xml"', shell=True)
The error which is shown is:
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'BCompare.exe "c:\Ref-2.xml" "c:\Cop-2.xml"' returned non-zero exit status 1
am I missing something? I tried different solutions to open command line instructions using this tutorial and many others but its not working.
Do something like this. Give Absolute path of .exe
check_output(absolute_path_of_beyond_compare "c:\Ref-2.xml" "c:\Cop-2.xml"', shell=True)
I am able to open the Beyond Compare using following code:
from subprocess import check_output
check_output("BCompare.exe Test1.txt Test2.txt", shell=True)
where BCompare.exe path is added in path variable and Test1.txt Test2.txt are present in the same directory from where i have executed the program.
import subprocess
subprocess.Popen([r"C:\Program Files\Beyond Compare 4\BCompare.exe", r"FullFilePath_File1", r"FullFilePath_File2"])
I tested on mine, and it works.
Instead of checkout, I am using Popen.
I am using Jupyter notebook by installing Anaconda3-5.2.0-Windows-x86_64
Python version = 3.6.5
use exact path where the beyond compare is installed or add that to your environment variable "Path".
in case using exact path of installation put something line "\"C:\Program Files\Beyond Compare 4\BCompare.exe\" test1.txt test2.txt"
the \" enables to read the special characters and extra spaces in path
I have a python script which takes the filename as a command argument and processes that file. However, i have thousands of files I need to process, and I would like to run the script on every file without having to add the filename as the argument each time.
for example:
process.py file1 will do exactly what I want
however, I want to run process.py on a folder containing thousands of files (file1, file2, file3, etc.)
I have found out it that it can be done simply in Bash
for f in *; do python myscript.py $f; done
However, I am on windows and don't want to install something like Cygwin.
What would a piece of code for the Windows command line look like that would emulate what the above Bash code accomplishes?
for %%f in (*.py) do (
start %%f
)
I think that'll work -- I don't have a Windows box handy at the moment to try it
How to loop through files matching wildcard in batch file
That link might help
import os, subprocess
for f in os.listdir('.'):
if os.path.isfile(f):
subprocess.call(["python", "myscript.py", f])
this solution will work on every platform, provided the python executable is in the PATH.
Also, if you want to recursively process files in nested subdirectories, you can use os.walk() instead of os.listdir()+os.path.isfile().
Since you have python, why not use that?
import subprocess
import glob
import sys
import os.path
for fname in glob.iglob(os.path.join('some-directory-name','*')):
proc = subprocess.Popen([sys.executable, 'myscript.py', fname])
proc.wait()
What's more, its portable.
For each file in current dir.
for %f in (*) do C:\Python34\python.exe "%f"
Update:
Note the quotes on the %f. You need them if your files contain spaces in the name. You can also put any path+executable after the do.
If we imagine your files look like:
./process.py
./myScripts/file1.py
./myScripts/file2.py
./myScripts/file3.py
...
In your example, would simply be:
for %f in (.\myScripts\*) do process.py "%f"
This would invoke:
process.py ".\myScripts\file1.py"
process.py ".\myScripts\file2.py"
process.py ".\myScripts\file3.py"