I am trying to unzip a number of files that are password protected but I keep getting some permission error. I have tried to perform this operation running vscode as an administrator but I am still getting the same error.
Here is the code:
input_file = ".\\pa-dirty-price-crawler\\folders"
import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
zip_ref.extractall(input_file, pwd=b'qpsqpwsr')
Here is the error:
Traceback (most recent call last):
File "c:/Users/usr/workspace/pa-dirty-price-crawler/src/outlook.py", line 23, in <module>
with zipfile.ZipFile(input_file, 'r') as zip_ref:
File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1240, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: '.\\pa-dirty-price-crawler\\folders'
I do not know of another library that can do this same operation but if anyone has suggestions in regards to getting this fixed I would really appreciate it.
Edit:
When I try to specify the entire file path name as so:
input_file = "C:\\Users\\usr\\workspace\\pa-dirty-price-crawler\\folders"
import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
zip_ref.extractall(pwd=b'qpsqpwsr')
I still get this error:
Traceback (most recent call last):
File "c:/Users/usr/workspace/pa-dirty-price-crawler/src/outlook.py", line 23, in <module>
with zipfile.ZipFile(input_file, 'r') as zip_ref:
File "C:\ProgramData\Anaconda3\lib\zipfile.py", line 1240, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\usr\\workspace\\pa-dirty-price-crawler\\folders'
It looks like you're passing a directory as the input. This is the likely problem, not that the zip is password protected.
To extract a zip file, zipfile.ZipFile takes a zip file as an input not a directory.
Therefore, your code needs two variables: an input zip file and an output directory:
input_file = r".\pa-dirty-price-crawler\folders\myzipfile.zip"
output_directory = r".\pa-dirty-price-crawler\folders"
import zipfile
with zipfile.ZipFile(input_file, 'r') as zip_ref:
zip_ref.extractall(output_directory, pwd=b'qpsqpwsr')
* note the use of r"string", this helps having to escape all your back slashes
Related
I'm trying to rename a set of files using the following code. The File is within the folder but it gives the following error. My code is attached here with:
import os
path='absolute_path'
arr = os.listdir(path)
for i in arr:
old_name=i
old_name_part=old_name.split(".")
new_name=old_name_part[0]+".png"
print(i,'\t',old_name,'\t',new_name)
os.rename(i,new_name)
Error :
drone_0002_01320.jpg drone_0002_01320.jpg drone_0002_01320.png
Traceback (most recent call last):
File "/absolute_path/rename.py", line 23, in <module>
os.rename(i,new_name)
FileNotFoundError: [Errno 2] No such file or directory: 'drone_0002_01320.jpg' -> 'drone_0002_01320.png'
os.rename(i,new_name) takes the full path of your file
you can change it into this:
os.rename(os.path.join(path, i),os.path.join(path, new_name))
Consider read about pathlib module, it has some good function for those kind of problems
Here is my sample Python script where I want to download a file from SFTP server to my local.
srv = pysftp.Connection(host=host, username=username, password=password, port=port, cnopts=connOption)
with srv.cd(sftppath):
data = srv.listdir()
try:
for infile in data:
print infile
srv.get(infile, destination, preserve_mtime=True)
I can connect successfully and it lists all files in the folder. But when I use srv.get() to download to my desktop I get following error,
IOError: [Errno 21] Is a directory: '/Users/ratha/Desktop'
Error stack;
Traceback (most recent call last):
File "/Users/ratha/PycharmProjects/SFTPDownloader/handler.py", line 9, in <module>
main()
File "/Users/ratha/PycharmProjects/SFTPDownloader/handler.py", line 5, in main
downloadSFTPFiles()
File "/Users/ratha/PycharmProjects/SFTPDownloader/Utilities/SFTPConnector.py", line 49, in downloadSFTPFiles
srv.get(infile, destination, preserve_mtime=True)
File "/Users/ratha/PycharmProjects/SFTPDownloader/venv/lib/python2.7/site-packages/pysftp/__init__.py", line 249, in get
self._sftp.get(remotepath, localpath, callback=callback)
File "/Users/ratha/PycharmProjects/SFTPDownloader/venv/lib/python2.7/site-packages/paramiko/sftp_client.py", line 801, in get
with open(localpath, "wb") as fl:
IOError: [Errno 21] Is a directory: '/Users/ratha/Desktop'
What Im doing wrong here?
The stack trace is actually really clear. Just focus on these two lines:
with open(localpath, "wb") as fl:
IOError: [Errno 21] Is a directory: '/Users/ratha/Desktop'
Clearly, pysftp tries to open /Users/ratha/Desktop as a file for binary write, which didn't go well because that is already, well, a directory. The documentation will confirm this:
localpath (str) – the local path and filename to copy, destination. If not specified, file is copied to local current working directory
Therefore you need to figure out the file name you want to save as, and use (best practice) os.path.join('/Users/ratha/Desktop', filename) to get a path and filename, instead of just a path.
Your destination variable should hold the path to the target file, with the file name included. If the target file name is to be the same as the source file name, you can join the base name of the source file name with the target directory instead:
import os
...
srv.get(infile, os.path.join(destination, os.path.basename(infile)), preserve_mtime=True)
I am trying to open a txt file for reading with this code:-
type_comments = [] #Declare an empty list
with open ('society6comments.txt', 'rt') as in_file: #Open file for reading of text data.
for line in in_file: #For each line of text store in a string variable named "line", and
type_comments.append(line.rstrip('\n')) #add that line to our list of lines.
Error:-
Error - Traceback (most recent call last):
File "c:/Users/sultan/python/society6/society6_promotion.py", line 6, in <module>
with open ('society6comments.txt', 'rt') as in_file:
FileNotFoundError: [Errno 2] No such file or directory: 'society6comments.txt'
I already have a file name with 'society6comments.txt' in the same directory has my script so why is it showing error?
The fact that the text file is in the same directory as your program does not make that directory the current working directory. Put the full path to the file in your open() call.
You can use os.path.dirname(__file__) to obtain the directory name of the script, and then join the file name you want:
import os
with open (os.path.join(os.path.dirname(os.path.abspath(__file__)), 'society6comments.txt'), 'rt') as in_file:
I am facing a PermissionError while I am trying to extract a zip file. I have gone through a lot of discussion threads here on SO but still unable to solve my issue.
Currently I am working with Python 3.6.1 on a Windows 8 box. I have created a new directory through the following code:
import os,zipfile
newpath = 'C:\\home\\vivvin\\shKLSE'
#newpath = r'C:\\home\\vivvin\\shKLSE'
if not os.path.exists(newpath):
os.makedirs(newpath)
Next I have downloaded a zip file and saved into newpath directory.
Now I am trying to extract all the files (10 csv files) within the zip file to be extracted into the newpath directory. To achieve that I have written the following code:
import os,zipfile
newpath = 'C:\\home\\vivvin\\shKLSE'
path_to_zip_file = newpath
directory_to_extract_to = newpath
#zip_ref = zipfile.ZipFile(newpath, 'r')
zip_ref = zipfile.ZipFile(newpath, 'w')
zip_ref.extractall(newpath)
zip_ref.close()
But each time I am getting an error as:
Traceback (most recent call last):
File "C:/Users/AtechM_03/PycharmProjects/Webinar/SeleniumScripts/extract.py", line 6, in <module>
zip_ref = zipfile.ZipFile(newpath, 'w')
File "C:\Python\lib\zipfile.py", line 1082, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:\\home\\vivvin\\shKLSE'
I have observed the properties manually of the zip file and seems there is a Security message along with a Unblock button. As of now I am clueless how to Unblock it.
Can anyone help me out please? Thanks in advance.
I had a similar problem trying to write to a file.
The fix that worked for me:
Right-click your PyCharm application and run it as administrator.
I run into the same error when unzipping the folder "temp.zip" and only extract the file .
In my case I had a directory with a folder "temp" and a zip file called "temp.zip".
def unzip(path, filename):
with ZipFile(path, 'r') as zipobj:
zipobj.extract(member=filename)
When I run this file, I got the error message:
...
"test.py", line 259, in unzip
with ZipFile(path, 'r') as zipobj:
File "C:\Program Files\Python36\lib\zipfile.py", line 1090, in __init__
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied
The problem is, that zipobj.extract() needs to create a folder called temp and extract the content (temporarly). But this folder already exists. --> There I got the permission denied error.
Solution:
either delete the folder "temp"
or move the zip first to another directory and unzip it there
I guess it's because your dictionary is on the C drive (the windows disk ,sometimes it's forbidden to write and erase) , if you changed to the D drive, it maybe work .
I write a basic file write code -
f = open('workfile', 'r+')
f.write('0123456789abcdef')
I run the file at cmd at the same folder where I put workfile.txt file but I get the error -
IOError: [Errno 2] No such file or directory: 'workfile'
I tried also to convert to exe file with py2exe and run the exe file but nothing...
what is the problem?
thanks!
====================
the full error massage when check on compiler-
Traceback (most recent call last):
File "/home/ubuntu/workspace/.c9/metadata/workspace/2.py", line 1, in <module>
f = open('workfile', 'r+')
IOError: [Errno 2] No such file or directory: 'workfile'
rename 'workfile' to 'workfile.txt':
f = open('workfile.txt', 'r+')