How to read different configuration file based on option - python & command line - python

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

Related

How can I use files/directories in python on Linux that start with .?

I'm a COMPLETE beginner to python, I'm making my first python script that really does anything. I'm assigning a directory to a variable so I can use it to move a file, but the directory includes a folder starting with . and python says it's invalid syntax. So how can I get python to ignore the .?
EDIT: Here's the code
#!/usr/bin/env python
import os
Optifine = find /home/Sol33t303/.minecraft -name
OptiFine_1.10.2_HD_U_E3.jar
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
You are mixing two very different things.
Are you writing Python or Bash, cause this is totally Bash:
Optifine = find /home/Sol33t303/.minecraft -name
You can't just run Bash commands inside a Python script!
If, for example you want to run a shell command inside your script and get its output you can use:
Optifine = subprocess.check_output(['find', '/home/Sol33t303/.minecraft', '-name'])
And then you split the output by line, and foreach line (file found), move it to the desired destination:
for line in Optifine.splitlines():
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")

Calling python commands from another python script

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

command line python script run on a file in different directory

I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present dir.
Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.
$ python script.py --script; /Users/admin/Desktop/file.txt
raise StopIteration('because of missing file (file.txt)')
StopIteration: because of missing file (file.txt)
Remove the semicolon because that will prematurely terminate the command.
Pass the correct path to the file to your program. You say it is /Users/admin/Desktop/folder/file.txt, however, your command is using /Users/admin/Desktop/file.txt (it's missing folder)
So the command should (probably) be:
$ python script.py --script /Users/admin/Desktop/folder/file.txt
If that doesn't work you will need to edit your question to show your code.

Passing parameters to .exe file within cmd

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)

Issuing commands to the command line in Python

This is my code. I'm pretty new to this.
from subprocess import call
call(["cd", "/etc/apache2/"])
However, when this function is run, I get
Errno 2: No such file or directory
I am running Django within Apache*. This is my views.py file. Ask for additional code, and you shall receive.
edit - It should be noted that /etc/apache2/ does indeed exist.
If you want to change the working directory of the Python process you can use chdir from the os module:
import os
os.chdir('/etc/apache2')
First of all, you will not get what you expect if you run this. Try
import os
os.chdir('/etc/apache2')
Second, try /path/to/cd as process may not know cd alias.

Categories