I would like to do a very simple thing but I am quite lost.
I am using a program called Blender and I want to write a script in python which open a .blend file but using the blender.app which is located in the same folder with the blend file, not with the blender.app which is located in Applications. (using Macosx)
So I was thinking that this should do the job...but instead it opens blender twice...
import os
path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open blender.app Import_mhx.blend")
I also tried this one
import os
path = os.getcwd()
print(path)
os.system("cd path/")
os.system("open Import_mhx.blend")
but unfortunately it opens the .blend file with the default blender.app which is located in Applications...
any idea?
This cannot work since the system command gets executed in a subshell, and the chdir is only valid for that subshell. Replace the command by
os.system("open -a path/blender.app Import_mhx.blend")
or (much better)
subprocess.check_call(["open", "-a", os.path.join(path, "blender.app"),
"Import_mhx.blend"])
Have you tried telling the open command to open it WITH a specific application?
open -a /path/to/blender.app /path/to/Import_mhx.blend
Your first attempt was on the right track but you were really telling open to just open two different things. Not one with the other.
Related
My question is I am unable to run a python file in VS Code terminal unless I specify the full path.
Whereas, when I see any python tutorial, only python filename is entered and it works.
Can somebody pls help me with this issue?
The files need to be in the same folder where you are operating / or have the other Python files.
You can use two ways for this:
first:
import os
pwd = os.getcwd()
# again, make sure you file is in the same directory.
the_file = (pwd + "\\filename.xlsx")
or Secondly,
#the below you can use wherever your file is and it will locate it.
# you specify the full path using pathlib.path and:
pathlib.path(directory here without the file itself).joinpath(file name here).
[Introduction]
Hi! I'm working on a python script to automate installation of UWP-Apps, it's been a long time i'm not touching Python; until this day. The script uses Depedencies inside the script directory, i've looking up on my older scripts and found this specific code.
os.chdir(os.path.dirname(sys.argv[0]))
[Problematic]
However, using the above code doesn't work on my current script but it's working fine on older scripts. When using above, it shows:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''
Already looking up on Internet about this topic; but most of them was talking about running the script from outer/different directory that leads me to dead end.
Any helps is appreciated :)
The easiest answer is probably to change your working directory, then call the .py file from where it is:
cd path/to/python/file && python ../.py
Of course you might find it even easier to write a script that does it all for you, like so:
Save this as runPython.sh in the directory where you're running the python script from, is:
#!/bin/sh
cd path/to/python/file
python ../script.py
Make it executable (for yourself):
chmod +x ./runPython.sh
Then you can simply enter your directory and run it:
./runPython.sh
If you want to only make changes to the python script:
mydir = os.getcwd() # would be the folder where you're running the file
mydir_tmp = "path/to/python/file" # get the path
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd()
The reason you got an error was because sys.argv[0] is the name of the file itself, instead you can pass the directory as a string and use sys.argv[1] instead.
import os
from os.path import abspath, dirname
os.chdir(dirname(abspath(__file__)))
You can use dirname(abspath(__file__))) to get the parent directory of the python script and os.chdir into it to make the script run in the directory where it is located.
Basically, I want to call a python program from anywhere using shell so I added a shebang and copied it to /usr/local/bin with executable permission. The python program takes a command line argument which is the relative path of an input file.
I am stuck here, I have no idea what to do so that I can obtain the absolute path of the shell. I am assuming once I get the absolute path somehow, I can use sys.argv[1] to get the entered relative path of the file(which I will append to the absolute path of shell working directory) but please do correct me it won't work.
You can Print Working Directory via os.environ['PWD']. Content of your_script.py:
#!/usr/bin/python3.5
import os
print(os.environ['PWD'])
Usage:
sanyash#sanyash-ub16:/etc/nginx$ your_script.py
/etc/nginx
sanyash#sanyash-ub16:/etc/nginx$
I made a script that when clicked on it copies all the files of the directory where it was opened in on to a USB.
It works inside Pycharm but when I convert it to an executable (When I use pyinstaller to convert the .py to a .exec) it does not work.
I’m almost certain I know what’s wrong but I don’t know how to fix it.
import shutil
import os
current = os.getcwd()
list_of_files = os.listdir(current)
def get_files():
print('CURRENT: ' + current)
print('File_List: ' + str(list_of_files))
for files in list_of_files:
shutil.copy(current + '/' + files, '/Volumes/U/Copy_things')
get_files()
Long story short I’m using os.getcwd() so the file knows where it is located.
When I execute the file in Pycharm the current directory that os.getcwd() gives me is
CURRENT: /Users/MainFrame/Desktop/python_test_hub/move_file_test
But when I open the executable (same folder as the .py file ) and the terminal opens up os.getcwd() gives me is
CURRENT: /Users/MainFrame
So I need to find a way for the executable to open up the terminal where it is located so it can copy those files.
I want to be able to execute it from any folder and copy the files to a USB.
os.getcwd() gets the directory of where the script is executed from, and this isn't necessarily where you're script is located. Pycharm is most likely altering this path when executing the script, as it executes your script from the project path, rather than the python path.
Try os.path.abspath(os.path.dirname(os.sys.argv[0])) instead of os.getcwd().
These answers have more information: os.getcwd() vs os.path.abspath(os.path.dirname(__file__))
Difference between __file__ and sys.argv[0]
I have a python script to open maya file and I want to set the project before opening the file ?
I try to find a file or an environement variable which is defining the current project path but I don't if it exists, do you know how I can do it ?
So you have a python script that opens the maya file. The file that sets the project is workspace.mel
import maya.cmds as cmds
cmds.workspace("workspace_directory", openWorkspace=True)
the cmds.workspace() command is what sets your project. "workspace_directory" is the path to where the workspace.mel command lives. You can use os.walk with the topdown=False flag in conjunction with an fnmatch to walk up directories from the desired scene file and search until it finds the workspace.mel file, and store that directory to a string. Then pass that into the "workspace_directory" argument I outline above!
https://knowledge.autodesk.com/support/maya/learn-explore/caas/CloudHelp/cloudhelp/2015/ENU/Maya/files/Interface-overview-Start-Maya-from-the-command-line--htm.html
You have all files describe in the doc. You may be interrested into :
-command [mel command]
-proj [dir]
-script [file]