i am a web developer (php, js, css and ...).
i order a python script for remove image background. it worked in cmd very well but when running it from php script, it dosnt work.
i look at the script for find problem and i realized that the script stops at this line:
net.load_state_dict(self.torch.load(os.path.join("../library/removeBG/models/", name, name + '.pth'), map_location="cpu"))
I guess the problem with the script is that it can't find the file, and probably the problem is caused by the path that os.path points to.
Is it possible to print the path that os .path points to?
If not, do you have a solution to this problem?
This should be enough:
name = 'name'
p = os.path.join("../library/removeBG/models/", name, name + '.pth')
print(p)
This is what i get:
>>> ../library/removeBG/models/name/name.pth
The problem here is that the php script might be in different directory so while executing the python script via php script, the os.path points to the directory from where it is being executed i.e. the location of php script.
TLDR; Try using absolute path.
Related
So, I want to make a python program which can locate and open Discord & Minecraft and open it on other computers.
And itself it's pretty easy to do if you have a directory in which the files are located, but my problem is I want to share this file with other people, and I want it to work on their PC too. This is my code:
import subprocess
subprocess.run("C:\\Users\\1\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe")
subprocess.run("E:\Geim\Minecraft Launcher\MinecraftLauncher.exe")
My Minecraft location is sligtly weird cause I need more memory so look only only on the discord example, if the solution would be replacing the users folder I'll deal with that
So the problem with this code again is that it works only on my computer since my users directory is named "1"
I also tried using os and retrieving the name which windows identifies Minecraft using Winapps module ("Minecraft Launcher") and while it works for chrome os can't read spaces and replacing it with "-" or "_", or simply lowercasing the letters didn't work
import os
os.system("minecraft launcher") # doesn't work
I also found this intersting chunk of code but I couldn't implement it in directory in the subprocess
from pathlib import Path
home = str(Path.home())
subprocess.run("C:\\Users\\" + home + "\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe") # doesn't work
It seems to be an easy problem for more skilled programmers so I hope to see help soon, thank you.
Edit: I was able to solve the problem by using pathlib and replacing "/" with "\", however I'd still like to receive an answer where it can be found in any not set directory (like mine "E\Geim...)
Here's my working code:
import subprocess
from pathlib import Path
home = str(Path.home())
home = home.replace("\\", "/")
subprocess.run(home + "\\AppData\\Local\\Discord\\Update.exe --processStart Discord.exe")
subprocess.run(home + "\\AppData\\Roaming\\.minecraft\\TLauncher.exe")
I want to get a number from the filepath of the current file in Sikuli - Jython
I have a python example of what i'm trying to achive.
In the example the path is:
C:\PycharmProjects\TestingPython\TestScripts\TestScript_3.sikuli\TestScript.py
import os
PointerLeft = "Script_"
PointerRight = ".sikuli"
FilePath = os.path.dirname(os.path.abspath(__file__))
NumberIWant = FilePath[FilePath.index(PointerLeft) + len(PointerLeft):FilePath.index(PointerRight)]
print(NumberIWant)
So what i want to get is the number 3. In python the example above works, but I cant use the __file__ ref in Sikulix. Nor does the split of the string work, so even if I get the string of the path, I still have to get the number.
Any help and/or ideas is greatly appreciated
Important:
the .py file in a .sikuli folder must have the same name
hence in your case: ...\TestScript_3.sikuli\TestScript_3.py
It looks like you are trying to run your stuff in the PyCharmcontext. If you do not run the script in the SikuliX IDE, you have to add from sikuli import * even to the main script.
To get the file path of the script use getBundlePath().
Then os.path(getBundlePath()).basename() will result to the string "TestScript_3.sikuli".
RaiMan from SikuliX
Well I searched a lot and found different ways to open program in python,
For example:-
import os
os.startfile(path) # I have to give a whole path that is not possible to give a full path for every program/software in my case.
The second one that I'm currently using
import os
os.system(fileName+'.exe')
In second example problem is:-
If I want to open calculator so its .exe file name is calc.exe and this happen for any other programs too (And i dont know about all the .exe file names of every program).
And assume If I wrote every program name hard coded so, what if user installed any new program. (my program wont able to open that program?)
If there is no other way to open programs in python so Is that possible to get the list of all install program in user's computer.
and there .exe file names (like:- calculator is calc.exe you got the point).
If you want to take a look at code
Note: I want generic solution.
There's always:
from subprocess import call
call(["calc.exe"])
This should allow you to use a dict or list or set to hold your program names and call them at will. This is covered also in this answer by David Cournapeau and chobok.
You can try with os.walk :
import os
exe_list=[]
for root, dirs, files in os.walk("."):
#print (dirs)
for j in dirs:
for i in files:
if i.endswith('.exe'):
#p=os.getcwd()+'/'+j+'/'+i
p=root+'/'+j+'/'+i
#print(p)
exe_list.append(p)
for i in exe_list :
print('index : {} file :{}'.format(exe_list.index(i),i.split('/')[-1]))
ip=int(input('Enter index of file :'))
print('executing {}...'.format(exe_list[ip]))
os.system(exe_list[ip])
os.getcwd()+'/'+i prepends the path of file to the exe file starting from root.
exe_list.index(i),i.split('/')[-1] fetches just the filename.exe
exe_list stores the whole path of an exe file at each index
Can be done with winapps
First install winapps by typing:
pip install winapps
After that use the library:
# This will give you list of installed applications along with some information
import winapps
for app in winapps.list_installed():
print(app)
If you want to search for an app you can simple do:
application = 'chrome'
for app in winapps.search_installed(application):
print(app)
Looking to make some use out of Python! I wanted to write a script to move all those carelessly placed .jpg files from my desktop.
Thing, is the script I'm using doesn't seem to be finding anything.
thoughts?
import os, shutil, glob
dst_fldr = "~/path/Desktop/newfolder";
for jpg_file in glob.glob("~/path/Desktop"+"\\*.jpg"):
print jpg_file + "will be moved to " + dst_fldr
shutil.move(jpg_file, dst_fldr);
~ is not a character that glob understands (it's a character that bash understands and expands). You'll have to provide a full path.
dst_fldr = "/path/to/Desktop/newfolder";
In addition, you'd want to modify the wildcard search, to something like this:
glob.glob("/path/to/Desktop/*.jpg"):
If your python script resides in Desktop, you can drop the /path/to/Desktop part of the path in both cases.
With these changes in place, I believe you're good to go.
Currently on a project that my client needs the reference file path to
remain in linux format. For example
A.ma , referencing objects from --> //linux/project/scene/B.ma
B.ma , referencing objects from --> //linux/project/scene/C.ma
Most of our Maya license here however are on Windows. I can run a
Python script that convert all the paths windows paths and save the
file. For example
Z:\project\scene\B.ma
However I'm trying to figure out a way to do this without converting
or altering the original file.... I'll try to explain what I'm trying to do.
Run the script to open the file.
The script checks for the linux formatted reference path, and all
child path down the hierarchy.
Maps all paths to their appropriate windows formatted paths.
Giving the animators the ability to "save" files normally without running a separate save script.
Is this possible to achieve this with Python script? Or will I need a
fully-compiled plug in to get this to work?
Any suggestion is greatly appreciated.
edit: Thank you for your input.
A little more clarification. The projects were set up for us by a remote company and part of the requirement is that we have to keep the path as is. They come as absolute path and we have no choice in that matter.
We match the mount //linux/ on our Fedora workstations. That same drive is mapped to Z:\ on our windows workstations. We only have 2 Maya license for Linux tho which is why I'm trying to do this.
Here is a solution. First step is to create a dict that keeps track of linux/windows references (don't forget to import the re module for regexp):
>>> def windows_path(path):
return path.replace('//linux', 'Z:').replace('/', '\\')
>>> reg = re.compile('(\w+\.ma) , referencing objects from --> (.*)')
>>> d = {}
>>> for line in open('D:\\temp\\Toto.txt'):
match = reg.match(line)
if match:
file_name = match.groups()[0]
linux_path = match.groups()[1]
d[file_name] = (linux_path, windows_path(linux_path))
>>> d
{'B.ma': ('//linux/project/scene/C.ma', 'Z:\\project\\scene\\C.ma'),
'A.ma': ('//linux/project/scene/B.ma', 'Z:\\project\\scene\\B.ma')}
Then you just need to loop on this dict to ask for file save:
>>> for file_name in d.keys():
s = raw_input('do you want to save file %s ? ' % file_name)
if s.lower() in ('y', 'yes'):
# TODO: save your file thanks to d[file][0] for linux path,
# d[file][1] for windows path
print '-> file %s was saved' % file_name
else:
print '-> file %s was not saved' % file_name
do you want to save file B.ma ? n
-> file B.ma was not saved
do you want to save file A.ma ? yes
-> file A.ma was saved
Many Windows applications will interpret paths with two leading "/"s as UNC paths. I don't know if Maya is one of those, but try it out. If Maya can understand paths like "//servername/share/foo", then all you need to do is set up a SMB server named "linux", and the paths will work as they are. I would guess that this is actually what your client does, since the path "//linux" would not make sense in a Linux-only environment.
You can use environment variables to do this. Maya will expand environment vars present in a file path, you could use Maya.env to set them up properly for each platform.
What you are looking for is the dirmap mel command. It is completely non-intrusive to your files as you just define a mapping from your linux paths to windows and/or vice versa. Maya will internally apply the mapping to resolve the paths, without changing them when saving the file.
To setup dirmap, you need to run a MEL script which issues the respective commands on maya startup. UserSetup.mel could be one place to put it.
For more details, see the official documentation - this particular link points to maya 2012, the command is available in Maya 7.0 and earlier as well though:
http://download.autodesk.com/global/docs/maya2012/en_us/Commands/dirmap.html