Trying to launch application through python - python

I am trying to launch one application through python. I am having problem with this can any give me a solution?
path1= "C:\\Program Files (x86)\\XYZ\\NX2\\RT900"
ver="7.50 Internal Release"
path2="bin\\Rt900.exe"
path3=os.path.join(path1,ver)
path4=os.path.join(path3,path2)
App.open("path4")
Can anyone tell me what's wrong in above statement?

you should join path1 with path2 not ver
path3 = os.path.join(path1, path2)
also you are doing path4=os.path.join(path3,path4) here path4 is used before assignment

As avasal has mentioned you should join paths correctly:
path3 = os.path.join(path1, path2)
It also looks like you need to open the path like so:
App.open(path3)
Note the lack of quotation marks in the last line.
Edit:
Seeing you keep changing the code in your question I have noted another error in your code:
path4=os.path.join(path3,path4)
This line will always file you are trying to join with a variable that doesn't even exist yet. A variable can't reference itself when it is being assigned for the first time. In this case you are doing os.path.join(path3, path4) but path4 doesn't even exist yet!

Related

Python - File Path not found if script run from another directory

I'm trying to run a script that works without issue when I run using in console, but causes issue if I try to run it from another directory (via IPython %run <script.py>)
The issue comes from this line, where it references a folder called "Pickles".
with open('Pickles/'+name+'_'+date.strftime('%y-%b-%d'),'rb') as f:
obj = pickle.load(f)
In Console:
python script.py <---works!
In running IPython (Jupyter) in another folder, it causes a FileNotFound exception.
How can I make any path references within my scripts more robust, without putting the whole extended path?
Thanks in advance!
Since running in the console the way you show works, the Pickles directory must be in the same directory as the script. You can make use of this fact so that you don't have to hard code the location of the Pickles directory, but also don't have to worry about setting the "current working directory" to be the directory containing Pickles, which is what your current code requires you to do.
Here's how to make your code work no matter where you run it from:
with open(os.path.join(os.path.dirname(__file__), 'Pickles', name + '_' + date.strftime('%y-%b-%d')), 'rb') as f:
obj = pickle.load(f)
os.path.dirname(__file__) provides the path to the directory containing the script that is currently running.
Generally speaking, it's a good practice to always fully specify the locations of things you interact with in the filesystem. A common way to do this as shown here.
UPDATE: I updated my answer to be more correct by not assuming a specific path separator character. I had chosen to use '/' only because the original code in the question already did this. It is also the case that the code given in the original question, and the code I gave originally, will work fine on Windows. The open() function will accept either type of path separator and will do the right thing on Windows.
You have to use absolute paths. Also to be cross platform use join:
First get the path of your script using the variable __file__
Get the directory of this file with os.path.dirname(__file__)
Get your relative path with os.path.join(os.path.dirname(__file__), "Pickles", f"{name}_{date.strftime('%y-%b-%d')}")
it gives you:
with open(os.path.join(os.path.dirname(__file__), "Pickles", f"{name}_{date.strftime('%y-%b-%d')}"), 'rb') as f:
obj = pickle.load(f)

os.join.path does change directory

Hello I have a piece of code where I want to join a path with a picture name which I am getting from an excel file. The thing is that my code changes my path string.
src=os.path.join('C:\\Users\\ekrem\\Desktop\\Distributions\\16-17',ws.cell(row=cell.row,column=2).value)
print('src=',src)
so here my src variable should be something like this:
C:\Users\ekrem\Desktop\Distributions\16-17\26231043_1686575684735993_7548330554586169888_n.jpg
but as you see I printed the src variable and I got this
src= C:\Users\ekrem\Desktop\16-17\26231043_1686575684735993_7548330554586169888_n.jpg
here the distribution folder is missing, have you any idea why?
Thank you in advance
In this case assuming the end piece is always a single filename it might be easier to do this with f-strings. That way there is no path weirdness that happens something like this should work (Assuming you're on python 3.6+, if you aren't then do this instead):
src = f'C:\\Users\\ekrem\\Desktop\\Distributions\\16-17\\{ws.cell(row=cell.row,column=2).value}'
print(f'{source=}') # Print source with label
P.S I would recommend switching the C:\\Users\\<name> with %USERPROFILE% (so %USERPROFILE%\\Desktop\\Distributions\\16-17\\), that way if you change accounts or hard-drives this still works.
Then just do validation with:
if os.path.exists(src):
# Do stuff
else:
raise FileNotFoundError(f"{src} file could not be found")

Invalid Syntax Error and looking through a directory and its subdirectories

I am super brand new to coding with python. For my work, we have ESRI maps (.MXDs) that need to be batch exported. The problem is that each map is in it's own folder within the main folder. I found a code to batch export my maps, if they are within the same directory (though it keeps giving an Invalid Syntax error). I also found a code that should look through all the subdirectories, but I do not know how to combine it with the first code.
Exporting my maps code (arcpy is how ArcMap uses python from what I gather):
import arcpy, os
arcpy.env.workspace = ws = r”C:\Users\Me\Desktop\Burn_Zones” #This is where I am getting that invalid syntax error!
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
pdf_name = mxd[:-4] + ".pdf"
arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
del mxd_list
So that's the first issue.
The code to look into all of the subdirectories is:
for root, dirs, files in os.walk(path):
for name in files:
if name.endswith((".html", ".htm")):
I don't think I would need the second for loop as the first code should be grabbing all of the .mxds for me. So would I only need the first for loop and chunk that above the mxd_list = arpy.ListFiles(".mxd") line of code?
Oh, and for that error line. I have tried the path name with C:\ and with Burn_Zones\ and all the combinations of those. That didn't work and that's the only thing I could figure.
Thank y'all so much for any help!
You're using the wrong quotes around your path. Is this your error?
arcpy.env.workspace = ws = r”C:\Users\162708\Desktop\Burn_Zones”
^
SyntaxError: invalid character in identifier
Replace those "quotes" with actual double quotes.
r”C:\Users\162708\Desktop\Burn_Zones” # wrong
r"C:\Users\162708\Desktop\Burn_Zones" # correct

Running python script with new directories

I have recently begun working on a new computer. All my python files and my data are in the dropbox folder, so having access to the data is not a problem. However, the "user" name on the file has changed. Thus, none of my os.chdir() operations work. Obviously, I can modify all of my scripts using a find and replace, but that won't help if I try using my old computer.
Currently, all the directories called look something like this:
"C:\Users\Old_Username\Dropbox\Path"
and the files I want to access on the new computer look like:
"C:\Users\New_Username\Dropbox\Path"
Is there some sort of try/except I can build into my script so it goes through the various path-name options if the first attempt doesn't work?
Thanks!
Any solution will involve editing your code; so if you are going to edit it anyway - its best to make it generic enough so it works on all platforms.
In the answer to How can I get the Dropbox folder location programmatically in Python? there is a code snippet that you can use if this problem is limited to dropbox.
For a more generic solution, you can use environment variables to figure out the home directory of a user.
On Windows the home directory is location is stored in %UserProfile%, on Linux and OSX it is in $HOME. Luckily Python will take care of all this for you with os.path.expanduser:
import os
home_dir = os.path.expanduser('~')
Using home_dir will ensure that the same path is resolved on all systems.
Thought the file sq.py with these codes(your olds):
C:/Users/Old_Username/Dropbox/Path
for x in range:
#something
def Something():
#something...
C:/Users/Old_Username/Dropbox/Path
Then a new .py file run these codes:
with open("sq.py","r") as f:
for x in f.readlines():
y=x
if re.findall("C:/Users/Old_Username/Dropbox/Path",x) == ['C:/Users/Old_Username/Dropbox/Path']:
x="C:/Users/New_Username/Dropbox/Path"
y=y.replace(y,x)
print (y)
Output is:
C:/Users/New_Username/Dropbox/Path
for x in range:
#something
def Something():
#something...
C:/Users/New_Username/Dropbox/Path
Hope its your solution at least can give you some idea dealing with your problem.
Knowing that eventually I will move or rename my projects or scripts, I always use this code right at the beginning:
import os, inspect
this_dir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
this_script = inspect.stack()[0][1]
this_script_name = this_script.split('/')[-1]
If you call your script not with the full but a relative path, then this_script will also not contain a full path. this_dir however will always be the full path to the directory.

Python: os.chdir() not working within a for loop?

I'm trying to get a homemade path navigation function working - basically I need to go through one folder, and explore every folder within it, running a function within each folder.
I reach a problem when I try to change directories within a for loop. I've got this "findDirectories" function:
def findDirectories(list):
for files in os.listdir("."):
print (files)
list.append(files)
os.chdir("y")
That last line causes the problems. If I remove it, the function just compiles a list with all the folders in that folder. Unfortunately, this means I have to run this each time I go down a folder, I can't just run the whole thing once. I've specified the folder "y" as that's a real folder, but the program crashes upon opening even with that. Doing os.chdir("y") outside of the for loop has no issues at all.
I'm new to Python, but not to programming in general. How can I get this to work, or is there a better way? The final result I need is running a Function on each single "*Response.xml" file that exists within this folder, no matter how deeply nested it is.
Well, you don't post the traceback of the actual error but clearly it doesn't work as you have specified y as a relative path.
Thus it may be able to change to y in the first iteration of the loop, but in the second it will be trying to change to a subdirectory of y that is also called y
Which you probably do not have.
You want to be doing something like
import os
for dirName, subDirs, fileNames in os.walk(rootPath):
# its not clear which files you want, I assume anything that ends with Response.xml?
for f in fileNames:
if f.endswith("Response.xml"):
# this is the path you will want to use
filePath = os.path.join(dirName, f)
# now do something with it!
doSomethingWithFilePath(filePath)
Thats untested, but you have the idea ...
As Dan said, os.walk would be better. See the example there.

Categories