Trying to zip files using commands in python.
When i run Compress-Archive logs logs.zip, it works on my powershell.
However, when i run the command on my python script, it is not working and does not even throw any error message.
cmd = "Compress-Archive logs logs.zip"
subprocess.call(["powershell.exe", cmd])
I am using this command because i do not have to install anything to zip a file.
Update: This command works. It didn't throw out any error message because it didn't even run in the first place. It was a silly mistake.
Use this code to zip a file
import zipfile
zip_file = zipfile.ZipFile('yourfile.zip', 'w')
zip_file.write('yourown.txt', compress_type=zipfile.ZIP_DEFLATED)
zip_file.close()
Related
I am trying to make a python script that runs the command line for turning a file into a .zip using python3 on my Mac.
However, whenever I run: os.system('zip -er file.zip /Users/mymac/Desktop/file.py') in python3, I get the error:
zip I/O error: Read-only file system
zip error: Could not create output file (file.zip)
I have tried disabling SIP on my Mac, as well as trying to use subprocess but I get the same message every time. I am really unsure why this happens... Is anyone able to help out?
i will suggest 3 steps !
first run :
fsck -n -f
then reboot !
make sure to run the python file as root
import os
try:
os.system('zip mag.zip mag.ppk')
print ('success')
except:
print ('problem')
screnshoot for my test
I'm trying to download a zip file off the web and trying to download it by console command using wget -O fileName urlLink, but when trying the code, CMD opens for a second then closes and I canno't find the file anywhere.
I've tried using other ways of getting the file downloaded, but they return ERROR 403. Using wget in CMD downloads the right file, but not in the python code.
def gotoDownload(link):
try:
with requests.Session().get(link) as download:
if isUrlOnline(download):
soup = BeautifulSoup(download.content, 'html.parser')
filtered = soup.find_all('script')
zip_file_url = re.search(r"('http.*?')", filtered[17].text).group().replace("'", "")
os.system("wget -O {0} {1}".format('CreatureFinalZTL.zip', zip_file_url))
Expect the file to download
Instead doesn't download anything.
There are a few things that may help here (it may or may not solve your problem, because it is dependent on the your machine's setup and configuration). First, one thing I would suggest is to be more specific on the paths. You can use absolute paths in the wget line like so:
"wget -O {0} {1}".format('/path/to/output/dir/CreatureFinalZTL.zip', zip_file_url)
This is usually helpful in case the Python environment does not operate in a directory you are expecting. Alternatively, you can force the directory with the following python command:
os.chdir( path )
Then, you can operate with relative paths without worry. A second thing I would suggest is to confirm that the url is what you are expecting. Just print it out like so:
print( zip_file_url )
It might sound silly, but it is important to confirm that your regex is operating correctly.
Use subprocess instead.
import subprocess
...
subprocess.run(["wget", "-O", 'CreatureFinalZTL.zip', zip_file_url])
This avoids any shell involvement with the command you wish to run.
Fixed, had to re-add wget to PATHS on windows.
When I run the following code I get an error named permission denied:
f=open('C:/Windows/System32/azm.txt','w')
f.write('+989193667998')
f.close()
On Windows, use backslashes instead of slashes to specify file paths:
f = open(r'c:\windows\system32\azm.txt', 'w')
And since you're trying to write to a system directory, make sure you run your code as Administrator.
I try to do the following:
1 VBA script calls shell using
RetVal = Shell(fullpythonexepath & fullscriptpath)
2 Shell get follwing command
fullpythonexepath & fullscriptpath
3 Python Script
import numpy as np
thefilepath = "input.txt" # is in the same folder as the script
theoutputpath = "output.txt" # is in the same folder as the script
ssc=np.loadtxt(thefilepath, delimiter='\t', skiprows=0, usecols=range(2)) #error line
#some other code to fit input data
np.savetxt(theoutputpath, sscnew, header=top, fmt='%1.2f\t%1.2f')
When I run this the output file doesn't get printed, which means the script doesn't run properly.
Now to the funny thing: When I run the python script from IDLE it runs fine. When I run it from the shell using the command:
fullpythonexepath & fullscriptpath
it says :
I tried inputting the fullpath to the input.txt and output.txt. When I do this and run it in the IDLE it does not find the file anymore. When called from the shell it doesn't work either.
Python obviousely does not find the path to the input.txt
the issue is, to my understanding, not related to VBA. The error occures also when using shell commands
Solution was to put :
import os
os.chdir(r"fullpath")
into my script this changes the current working directory to my path and then input.txt gets found.
When you start the script from shell the working directory for the script will be the directory from which it is called upon.
Most IDLE define their own working directory.
To check I suggest doing:
os.getcwd()
in both cases and look what directory is used in both cases
I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py