Python, unpacking a .jar file, doesnt work - python

So, I'm trying to unzip a .jar file using this code:
It won't unzip, only 20 / 500 files, and no folders/pictures
The same thing happens when I enter a .zip file in filename.
Any one any suggestions?
import zipfile
zfilename = "PhotoVieuwer.jar"
if zipfile.is_zipfile(zfilename):
print "%s is a valid zip file" % zfilename
else:
print "%s is not a valid zip file" % zfilename
print '-'*40
zfile = zipfile.ZipFile( zfilename, "r" )
zfile.printdir()
print '-'*40
for info in zfile.infolist():
fname = info.filename
data = zfile.read(fname)
if fname.endswith(".txt"):
print "These are the contents of %s:" % fname
print data
filename = fname
fout = open(filename, "w")
fout.write(data)
fout.close()
print "New file created --> %s" % filename
print '-'*40
But, it doesn't work, it unzips maybe 10 out of 500 files
Can anyone help me on fixing this?
Already Thanks!
I tried adding, what Python told me, I got this:
Oops! Your edit couldn't be submitted because:
body is limited to 30000 characters; you entered 153562
and only the error is :
Traceback (most recent call last):
File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'
The files that get unzipped:
amw.Class
amx.Class
amz.Class
ana.Class
ane.Class
anf.Class
ang.Class
ank.Class
anm.Class
ann.Class
ano.Class
anq.Class
anr.Class
anx.Class
any.Class
anz.Class
aob.Class
aoc.Class
aod.Class
aoe.Class

This traceback tells you what you need to know:
Traceback (most recent call last):
File "C:\Python27\uc\TeStINGGFDSqAEZ.py", line 26, in <module>
fout = open(filename, "w")
IOError: [Errno 2] No such file or directory: 'net/minecraft/client/ClientBrandRetriever.class'
The error message says that either the file ClientBrandRetriever.class doesn't exist or the directory net/minecraft/client does not exist. When a file is opened for writing Python creates it, so it can't be a problem that the file does not exist. It must be the case that the directory does not exist.
Consider that this works
>>> open('temp.txt', 'w')
<open file 'temp.txt', mode 'w' at 0x015FF0D0>
but this doesn't, giving nearly identical traceback to the one you are getting:
>>> open('bogus/temp.txt', 'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'bogus/temp.txt'
Creating the directory fixes it:
>>> os.makedirs('bogus')
>>> open('bogus/temp.txt', 'w')
<open file 'bogus/temp.txt', mode 'w' at 0x01625D30>
Just prior to opening the file you should check if the directory exists and create it if necessary.
So to solve your problem, replace this
fout = open(filename, 'w')
with this
head, tail = os.path.split(filename) # isolate directory name
if not os.path.exists(head): # see if it exists
os.makedirs(head) # if not, create it
fout = open(filename, 'w')

If python -mzipfile -e PhotoVieuwer.jar dest works then you could:
import zipfile
with zipfile.ZipFile("PhotoVieuwer.jar") as z:
z.extractall()

Related

Python suddenly cannot find files only when I want to rename or copy them

I'm trying to very simply copy a load of files from one directory to another and then rename the files (to replace whitespaces and %20 with _). In my root directory, I have:
optimiser.py (what I use to run the script)
Folder (named 'Files to Improve' containing basic text files)
Folder (named 'Finished' where I want every file to be copied to (empty))
It seems that the files can be found because this code successfully prints every file (and their respective new filenames):
import os
from shutil import copyfile
files_to_improve_dir = 'Files to Improve'
finished_files_dir = 'Finished'
for f in os.listdir(files_to_improve_dir):
new_filename = f.replace(' ','_').replace('%20','_').lower()
print('f:', f, '--- n:', new_filename)
The first problem occurs when I add copyfile(f, finished_files_dir) to the bottom, returning this error:
f: FILE.txt --- n: file.txt
Traceback (most recent call last):
File "optimiser.py", line 10, in <module>
copyfile(f, finished_files_dir)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 259, in copyfile
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
FileNotFoundError: [Errno 2] No such file or directory: 'FILE.txt'
Does anyone know what this error is and why it occurs?
Secondly, if I instead add this to the bottom instead of the copy file line:
if (f != new_filename):
os.rename(f, new_filename)
It returns this error instead:
f: FILE.txt --- n: file.txt
Traceback (most recent call last):
File "optimiser.py", line 12, in <module>
os.rename(f, new_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'FILE.txt' -> 'file.txt'
I don't know why it suddenly now cannot find the files, despite it printing them in the previous code. If it couldn't find the files, how could it have printed them?
Thanks for any help here. The complete code is below:
import os
from shutil import copyfile
files_to_improve_dir = 'Files to Improve'
finished_files_dir = 'Finished'
for f in os.listdir(files_to_improve_dir):
new_filename = f.replace(' ','_').replace('%20','_').lower()
print('f:', f, '--- n:', new_filename)
copyfile(f, finished_files_dir) #error 1
if (f != new_filename): #error 2
os.rename(f, new_filename) #error 2
Change your
copyfile(f, finished_files_dir) #error 1
to
copyfile(os.path.join(files_to_improve_dir, f),
os.path.join(finished_files_dir, f))
and
os.rename(f, new_filename) #error 2
to
os.rename(os.path.join(files_to_improve_dir, f),
os.path.join(files_to_improve_dir, new_filename))
One more hint: When renaming your file, there might araise duplicates (e.g. bla bla and bla%20bla will both be renamed to bla_bla), so you may want to check if the destination exists already...

Creating text file: IOError: [Errno 21] Is a directory: '/Users/muitprogram/PycharmProjects/untitled/HR/train.txt'

I am trying to write to create and write to a text file. However, the error
Traceback (most recent call last):
File "/Users/muitprogram/PycharmProjects/untitled/histogramSet.py", line 207, in <module>
drinktrainfile = open(abs_file_path, "w")
IOError: [Errno 21] Is a directory: '/Users/muitprogram/PycharmProjects/untitled/HR/train.txt'
shows up. There are other instances of this in Stack Overflow, however, none of these deal with creating and writing a new file. The directories all exist however- only the file is being created The code that does this is:
import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] # i.e. /path/to/dir/
rel_path = str(j) + "/train.txt" # HR/train.txt
abs_file_path = os.path.join(script_dir, rel_path) #/path/to/dir/HR/train.txt
drinktrainfile = open(abs_file_path, "w")
EDIT: train.txt shows up, except as a directory. How do I make it a text file?
The resource is actually a directory. It was very likely a mistake, as it is not likely that somebody would have created a directory by that name. First, remove that directory, and then try to create and open the file.
You can open the file with open('/Users/muitprogram/PycharmProjects/untitled/HR/train.txt', 'w'), assuming that the file does not already exist.

Python I/O error: How do I fix this program for file paths with whitespace?

When running the codes.
file_path = raw_input("Drag the text file here: ")
file_path = file_path.strip()
file_handle = open(file_path, 'r')
for line in file_handle:
print line
Output:
Drag the text file here: /Users/user_name/Desktop/white\ space/text.txt
Traceback (most recent call last):
File "desktop/test.py", line 3, in <module>
file_handle = open(file_path, 'r')
IOError: [Errno 2] No such file or directory: '/Users/user_name/Desktop/white\\ space/text.txt'
The program runs fine for any pathname without whitespace.
The immediate fix would be removing the escaped '\\' characters, file_path.strip().replace('\\', '')
This should return /Users/user_name/Desktop/white space/text.txt which is a valid path for you to use.
Look into os.path for ways to handle pathnames.

Python 3.4+ Scripting regarding to Batch Editing JSON and Saving them

I'm trying to Batch Edit all JSON files within a folder, and saving each of them as new JSON all at once, instead of doing each one.
def new_json(filename):
with open(filename, 'r+') as f:
json_data = json.load(f)
somefunction()
f.seek(0)
base, ext = os.path.splitext(filename)
out_file = open(base + '_expand' + ext, 'w')
json.dump(room_data, out_file, indent=4)
def batchRun(foldername):
for f in os.listdir(foldername):
new_json(f)
folder = 'testing'
batchRun(folder)
When I try to run the batchRun function, it gives me an error
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
batchRun(folder)
File "<pyshell#8>", line 3, in batchRun
new_json(f)
File "<pyshell#6>", line 2, in new_json
with open(filename, 'r+') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'simple_json.txt'
And I know for sure simple_json.txt and other files are within that folder that I defined, so I'm not sure what's happening.
os.listdir gives you a list of file names, not file paths. You can use os.path.join to get the full file path:
def batchRun(foldername):
for f in os.listdir(foldername):
new_json(os.path.join(foldername, f))

Python IOError: [Errno 2] from recursive directory call

The code below is part of a program I am writing that runs a method on every .py, .sh. or .pl file in a directory and its folders.
for root, subs, files in os.walk("."):
for a in files:
if a.endswith('.py') or a.endswith('.sh') or a.endswith('.pl'):
scriptFile = open(a, 'r')
writer(writeFile, scriptFile)
scriptFile.close()
else:
continue
When writing the program, it worked in the directory tree I wrote it in, but when I moved it to another folder to try it there I get this error message:
Traceback (most recent call last):
File "versionTEST.py", line 75, in <module>
scriptFile = open(a, 'r')
IOError: [Errno 2] No such file or directory: 'enabledLogSources.sh'
I know something weird is going on because the file is most definitely there...
You'll need to prepend the root directory to your filename
scriptFile = open(root + '/' + a, 'r')
files contains only the file names, not the entire path. The path to the file can be obtained by joining the file name and the root:
scriptFile = open(os.path.join(root, a), "r")
You might want to have a look at
https://docs.python.org/2/library/os.html#os.walk

Categories