how to fix read-only file system error python - python

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

Related

Zip file in windows using commands in python

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()

Run .py script via sh import module error

This is a very basic question on how to code in python and run your script from a very beginner.
I'm writing a script using Xcode9.4.1 which is supposed to be for python3.6. I then have an sh script run.sh, in the same folder of the script (say "my_folder") which simply looks like
python my_script.py
The python script looks like
from tick.base import TimeFunction
import numpy as np
import matplotlib.pyplot as plt
v = np.arange(0., 10., 1.)
f_v = v + 1
u = TimeFunction((v, f_v))
plt.plot(v, u.value(v))
print('donne!\n')
But as I try to run my_script.sh from the terminal I get a "ImportError: No module named tick.base" error.
But the tick folder is actually present in "my_computer/anaconda3/lib/python3.6/site-packages" and up to last week I was using Spyder from anaconda navigator and everything was correctly working, so no "import error" occurred.
The question is quite trivial, in some sense it simply is "what's the typical procedure to code and run python script and how modules are supposed to be imported-downloaded when running on a given machine?"
I need it since my script is to be run on another machine through ssh and using my laptop to make some attempts. Up to last year I used to work in C and only need to move some folders with code and .h files.
Thank for help!
EDIT 1:
From the Spyder 3.2.7 setting, where the script was giving non problem, I printed the
import sys
print(sys.path)
The -manually- copied the content to the sys.path variable in my_script.py and rerun 'run.sh' and now getting a new (strange) error:
Traceback (most recent call last):
[...]
File "/Users/my_computer/anaconda3/lib/python3.6/site-packages/tick/array/build/array.py", line 106
def tick_double_array_to_file(_file: 'std::string', array: 'ArrayDouble const &') -> "void":
^
SyntaxError: invalid syntax
First, check the python which you are calling the script with is pointing to the anaconda python and it is of the same version you are expecting it to be. You can do "which python" command in Linux and Mac to which the path which points to python. It if is pointing to some different version or build of python than the one which you are expecting then add the needed path to the system environment PATH variable. In Linux and Mac this can be done by adding the following line in the .bashrc file at the /home/ folder:
export PATH=/your/python/path:$PATH
And then source the .bashrc file.
source .bashrc
If you are on a operating system like cent os ,breaking the default python path can break your yum so be careful before changing it.
I am running a script in PyCharm and under the Project Interpretor I have the path
C:\envs\conda\keras2\python.exe
When I try to run the script via ssh on the server I get a 'no module named' error. I get
/usr/bin/python as the ans to 'which python' on the server itself. Could you tell me which path I must add for the script to run properly?

pylint with jenkins - complince that can't find xml file

I am trying to run pylint with jenkins with following command:
pylint -f parseable -d I0011,R0801 "mypath\highLevel" | tee.exe pylint.out
The process looks run fine, pylint.out created with a lot of information inside but during pylint report creation I get following error:
13:38:27 ERROR: Publisher hudson.plugins.violations.ViolationsPublisher aborted due to exception
13:38:27 java.io.FileNotFoundException: C:\Users\DMD\.jenkins\jobs\Diamond - Run Coverage\builds\2015-07-26_13-34-30\violations\file\A:\highLevel\Monitor\InitialBootAdapter.py.xml (The filename, directory name, or volume label syntax is incorrect)
It's creates very strange path:
C:\Users\DMD\.jenkins\jobs\Diamond - Run Coverage\builds\2015-07-26_13-34-30\violations\file\A:\highLevel\Monitor\InitialBootAdapter.py.xml
I don't really understand what happens.
Why pylint is interested in file InitialBootAdapter.py? Why it's looks for file InitialBootAdapter.py.xml? Who should create it and why? I searched for this file over all the environment and didn't find. But I did'nt find any xml for my other py files?
Maybe you have experience with pylint and can help?
Thank you.
I have experience with pylint in jenkins. And here is how I use it, hope it will help someone.
Step 1
Add a "Execute Shell" step and execute the pylint command to generate the pylint.out. Please note
/usr/local/bin/pylint -f parseable -d I0011,R0801 my-python-project-folder | tee pylint.out
Step 2
Make sure you have the Violation Report Plugin, after that , click Add post-build action-->Report Violation, put the pylint.out in the corresponding field.
And after the successful run, the pylint report looks like this:
I fixed the problem, it took time and DevOps help but it worked and is described in my own blog (it's more my online notebook than blog) in very small details.
The most important point in this post is small utility
import fileinput, sys
if __name__ == "__main__":
for line in fileinput.FileInput(sys.argv[1], inplace=True):
if ".cs" in line:
line = line.replace("\\", "/")
print line,
Here sys.argv[1] should be path to your violations.xml file.
You have to move the path as a command line argument to the utility as path to your violations.xml file is dynamic and depends on build id.

error while trying to install a package using the python-apt API

I found a code which i need. It is from this link : How to install a package using the python-apt API
#!/usr/bin/env python
# aptinstall.py
import apt
import sys
pkg_name = "libjs-yui-doc"
cache = apt.cache.Cache()
cache.update() # error is in this line
pkg = cache[pkg_name]
if pkg.is_installed:
print "{pkg_name} already installed".format(pkg_name=pkg_name)
else:
pkg.mark_install()
try:
cache.commit()
except Exception, arg:
print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))
However i can't make it work. I searched about the problem on the web. It is said that there should be no package manager,apt,pip etc active in order to work with this code. However, no package manager,apt,pip etc. is open in my computer. I thought that when computer starts, some package manager can be active. So i typed
ps -aux
in terminal and look at the active processes, but i didn't see any active process related to package manager(i'm not %100 sure about this, because any process i don't know can be related to package manager.But how could i know it?) To sum up,i started the computer and opened only terminal. Then i typed python aptinstall.py and hit enter. I take the following error :
Traceback (most recent call last):
File "aptinstall.py", line 7, in <module>
cache.update()
File "/usr/lib/python2.7/dist-packages/apt/cache.py", line 397, in update
raise LockFailedException("Failed to lock %s" % lockfile)
apt.cache.LockFailedException: Failed to lock /var/lib/apt/lists/lock
I delete the lock by giving the command in terminal :
sudo rm /var/lib/dpkg/lock
It didn't work too.
How can i solve this problem? Any idea will be appreciated.
Please try looking for update-manager in ps. It runs automatically on a periodic basis, so it may be locking the apt db.
There are three different reasons which cause this error.
1 - As i mentioned earlier, if any package manager is runnning(for example;pip,apt-get,synaptic,etc), it gives the error.
2 - If you are using your ubuntu in a virtual machine, this causes the same error.
3 - If you are running your program without root privileges, this causes the same error. For example ,if you are running your program with "python aptinstall.py" you get the error, running the program with "sudo python aptinstall.py" is the correct one.

Python+ubuntu error

Am trying to run the following python program
import re
regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")
f=open('out.txt')
for a in f:
print regex.findall(a)
print '\n'
when I type the code into the interpreter manually, it works as expected
but when i save it as a file and try to run it , it gives errors.
The command i used to run it is
chmod +x
sudo ./pymod.py
ERROR:
./pymod.py: 2: Syntax error: "(" unexpected
if i dont use sudo, the error i get is
./pymod.py: line 2: syntax error near unexpected token `('
./pymod.py: line 2: `regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")'
am using ubuntu 10.04 with everything on default
it takes about 10-15 seconds for the error to appear
Your file should start with shebang. You should include the path to the python interpreter
#!/usr/bin/env python
import re
regex=re.compile("http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]")
Check out : http://en.wikipedia.org/wiki/Shebang_(Unix)
This is probably executing as a bash script instead of in Python. Put
#!/usr/bin/env python
at the beginning of your script.
When you set something as executable, you have to specify what you want it to run it with, or Linux will consider it to be a bash script.
Add this as the first line of the file:
#!/usr/bin/python
Or run it like:
python pymod.py
Cheers!
Either use the "shebang". I.e. put
#! /usr/bin/python
as the first line of your script.
Or teach your ubuntu how to treat python scripts without it
as described here: http://www.daniweb.com/code/snippet241988.html

Categories

Resources