I'm calling Microsoft Ajax Minifier from Python like so:
minifyArguments = ["C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\AjaxMin.exe"]
for f in filesToMinify:
minifyArguments.append(f)
minifyArguments.append("–out")
minifyArguments.append("C:\\Users\\XXX\\Desktop\\TestFolder") #minifyJSDestinationPath
minifyArguments.append("–clobber")
ajaxMinProcess = subprocess.Popen(minifyArguments, shell=False)
stdout, stderr = ajaxMinProcess.communicate()
This works fine, and I see that it's starting etc. but when it wants to write the output file it gives an error:
AjaxMinifier.exe: error AM-AUTH: Access to the path 'C:\Users\XXX\Desktop\TestFolder' is denied.
I have tried different folders, the issue is not exclusive to the one in the code. It can't write to any folder.
When I don't call it from Python but directly from the commandline it works without problems.
Why does this happen and how can I fix it?
Thanks.
I found the solution to my problem:
This line:
minifyArguments.append("C:\\Users\\XXX\\Desktop\\TestFolder")
Should include the filename, like this:
minifyArguments.append("C:\\Users\\XXX\\Desktop\\TestFolder\\script.min.js")
Related
I'm attempting to execute WMIC on a remote host through the WMI module via Win32_Process.Create() function and save the output on said remote host via /output, more or > but neither of these is generating the expected output. Instead, I am receiving empty files.
I have tried using /output, more and > to achieve proper output but the resultant files are always blank. I know that I am creating processes with expected output otherwise because I have tried using 'ipconfig >' into a file on the remote host and it gives the expected output with network configuration data, it is only WMIC that is giving me trouble. I have also tried removing the 'cmd.exe /c' portion and just running directly with wmic.exe with no luck as well as specifying the exact executable location.
I've also tried using /Output:FILENAME alone and /Output:STDOUT with the redirector (>) to a file. None seem to work properly.
import wmi
session = wmi.WMI(computer="192.168.8.132", user="testing", password="testing")
process_startup = session.Win32_ProcessStartup.new()
command = r"wmic.exe /output:C:\Users\testing\WMIUsers0.txt /namespace:\\root\cimv2 UserAccount get * /value"
process_id, return_value = session.Win32_Process.Create(CommandLine=command, ProcessStartupInformation=process_startup)
print("Process Executed: "+command)
print("Process ID: "+str(process_id)+" , Return Value (0 = Success): "+str(return_value)+"\n")
The above code generates an empty file on the host at the expected location rather than the actual WMI returned data. I copy pasted the 'command' given above into the remote host and it works as expected for stdout as well as generating the data within the file.
This is only an approximation of my code but represents the issue well - what do I have to modify to have actual results populate in the destination file rather than a blank file when running remotely over the WMI module?
edit: Using ProcessHacker on the target, I can see my process started with the parameters 'cmd.exe /c wmic /output:"C:\Users\testing\WMIUsers0.txt" /namespace:\root\cimv2 UserAccount get * /value' - If I copy paste this into a separate cmd window, it works as expected and generates the file but when I run it remotely, even though it has the proper parameters, it generates an empty file. I am going insane trying to figure out why this isn't working..
Edit2: I've tried to many different variations utilizing subprocess.call, check_output and other things and am having no luck trying to do this in Python. Today is not my day.
Edit3: Decided to just use subprocess.getoutput and store the data locally because getting it to execute remotely was driving me insane - No idea why it won't work after inspecting what was executing - same exact cmdline window parameters but one generates blank file, other was dataful. Crazy, has to be some functionality of the WMI module preventing proper transfer..or something...no idea.
I need to extract text from a PDF. I tried the PyPDF2, but the textExtract method returned an encrypted text, even though the pdf is not encrypted acoording to the isEncrypted method.
So I moved on to trying accessing a program that does the job from the command prompt, so I could call it from python with the subprocess module. I found this program called textExtract, which did the job I wanted with the following command line on cmd:
"textextract.exe" "download.pdf" /to "download.txt"
However, when I tried running it with subprocess I couldn't get a 0 return code.
Here is the code I tried:
textextract = shlex.split(r'"textextract.exe" "download.pdf" /to "download.txt"')
subprocess.run(textextract)
I already tried it with shell=True, but it didn't work.
Can anyone help me?
I was able to get the following script to work from the command line after installing the PDF2Text Pilot application you're trying to use:
import shlex
import subprocess
args = shlex.split(r'"textextract.exe" "download.pdf" /to "download.txt"')
print('args:', args)
subprocess.run(args)
Sample screen output of running it from a command line session:
> C:\Python3\python run-textextract.py
args: ['textextract.exe', 'download.pdf', '/to', 'download.txt']
Progress:
Text from "download.pdf" has been successfully extracted...
Text extraction has been completed!
The above output was generated using Python 3.7.0.
I don't know if your use of spyder on anaconda affects things or not since I'm not familiar with it/them. If you continue to have problems with this, then, if it's possible, I suggest you see if you can get things working directly—i.e. running the the Python interpreter on the script manually from the command line similar to what's shown above. If that works, but using spyder doesn't, then you'll at least know the cause of the problem.
There's no need to build a string of quoted strings and then parse that back out to a list of strings. Just create a list and pass that:
command=["textextract.exe", "download.pdf", "/to", "download.txt"]
subprocess.run(command)
All that shlex.split is doing is creating a list by removing all of the quotes you had to add when creating the string in the first place. That's an extra step that provides no value over just creating the list yourself.
I am new at python and learning the language. The following code should create a file in the running program directory and write to it but it doesn't do this at all in a .py file. If I put the same code in the IDLE shell it returns 17. No errors just doesn't create the file. What am I doing wrong?
with open("st.txt", "w") as f:
f.write("Hi from Python!")
Thanks for the help
Mike
This code is flawless, no problem!
I guess that in your REPL shell, the $PWD environment variable is set for somewhere, so your destination file is in some corner.
No exception thrown indicates that no problem with access authority.
Maybe you can set some absolute path string, such as ~/st.txt
By the way, the successful invoke should return 15 instead of 17, totally count 15 chars.
your code works well, st.txt will be touched at executing path.
other ways, your system account can't write in your execute path.
try in your $HOME path to execute your code, I think, It will work well
I spend a few hours writing a little script.
Basically what it does is create a new text file and fills it up with whatever.
I zip the text file --using zipfile-- and here's where my problem lies.
I want to run the Windows system command:
copy /b "imgFile.jpg" + "zipFile.zip" newImage.jpg
To merge the image "imgFile.jpg" and the zip "zipFile.zip".
So:
os.system("copy /b \"imgFile.jpg\" + \"zipFile.zip\" newImage.jpg")
When I run my script, it all seems to go fine.
But when it's done and I try to extract the 'newImage.jpg' file, it gives me:
The archive is either in unknown format or damaged
This ONLY happens when I run the system command within the script.
It works fine when I use the shell. It even works if I use a separate script.
I've double checked my zip file. Everything is in good shape.
Is there something I'm doing wrong? Something I'm not seeing?
Have you tried using shutil?
import shutil
shutil.copy(src, dst)
There may be a problem with the way Python is passing the arguments to the shell command. Try using subprocess.call. This method takes arguments as an array and passes them that way to the command:
import subprocess
subprocess.call(["copy", "/b", '"imgFile.jpg" + "zipFile.zip"', "newImage.jpg"])
Im using the plone cms and am having trouble with a python script. I get a name error "the global name 'open' is not defined". When i put the code in a seperate python script it works fine and the information is being passed to the python script becuase i can print the query. Code is below:
#Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
# Insert data that was passed from the form
query=request.query
#print query
f = open("blast_query.txt","w")
for i in query:
f.write(i)
return printed
I also have a second question, can i tell python to open a file in in a certain directory for example, If the script is in a certain loaction i.e. home folder, but i want the script to open a file at home/some_directory/some_directory can it be done?
Python Scripts in Plone are restricted and have no access to the filesystem. The open call is thus not available. You'll have to use an External Method or full python module to have full access to the filesystem.