deleting a file after uploading to s3 in python - python

def upload(s):
conn=tinys3.Connection("AKIAJPOZEBO47FJYS3OA","04IZL8X9wlzBB5LkLlZD5GI/",tls=True)
f = open(s,'rb')
z=str(datetime.datetime.now().date())
x=z+'/'+s
conn.upload(x,f,'crawling1')
os.remove(s)
The file is not deleting after i upload to s3 it is not deleting in the local directory any alternate solutions?

You have to close the file before you can delete it:
import os
a = open('a')
os.remove('a')
>> Traceback (most recent call last):
File "main.py", line 35, in <module>
os.remove('a')
PermissionError: [WinError 32] The process cannot access the file because
it is being used by another process: 'a'
You should add f.close() before the call to os.remove, or simply use with:
with open(s,'rb') as f:
conn = tinys3.Connection("AKIAJPOZEBO47FJYS3OA","04IZL8X9wlzBB5LkLlZD5GI/",tls=True)
z = str(datetime.datetime.now().date())
x = z + '/' + s
conn.upload(x, f, 'crawling1')
os.remove(s)

Related

FileNotFoundError when sorting files by size

I am trying create a list of image file names, sorted by the size of the file:
path = '/home/onur/Desktop/dataset/'
images = sorted(os.listdir(path), key = os.path.getsize)
print(images)
But I'm getting this error:
Traceback (most recent call last):
File "/home/onur/Desktop/image-downloader.py", line 98, in <module>
images = sorted(os.listdir(path), key = os.path.getsize)
File "/usr/lib/python3.9/genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: 'image_535.jpg'
The python file is NOT in /home/onur/Desktop/dataset/. It is just in Desktop so I wonder if that is the reason for the error. If so, how can I fix it?
You are correct. The problem is that os.path.getsize raises an error if the file does not exist. Because your Python script is in /home/onur/Desktop and the file name passed to os.path.getsize is just image_535.jpg, os.path.getsize looks for the file in your Desktop directory. Since the file is not there, os.path.getsize raises the error. You could run this to correctly test the file size.
path = '/home/onur/Desktop/dataset/'
images = sorted(os.listdir(path), key=lambda f: os.path.getsize(os.path.join(path, f)))
print(images)

"[WinError 6] The handle is invalid" with Urllib

import urllib.request
def Download(url, file_name):
urllib.request.urlretrieve(url, file_name)
f = open("links.txt", "r")
lines = f.readlines()
for line in lines:
for x in range (1, 5):
filenaame = x
cut_string = line.split('?$')
new_string = cut_string[0]
numerator = new_string.split('/1/')
separator = ''
link = (separator.join(numerator[0] + "/{}/".format(x) + numerator[1]))
file_name = link.split('/{}/'.format(x))
file_name = file_name[1]
file_name = file_name.split('.')
file_name = (separator.join(file_name[0] + "{}".format(filenaame)))
filenaame =+ 1
print("Downloading: {}".format(file_name))
Download(link, filenaame)
Error:
Traceback (most recent call last):
File "C:\python\downloader\rr.py", line 29, in <module>
Download(link, filenaame)
File "C:\python\downloader\rr.py", line 5, in Download
urllib.request.urlretrieve(url, file_name)
File "C:\python\lib\urllib\request.py", line 258, in urlretrieve
tfp = open(filename, 'wb')
OSError: [WinError 6] The handle is invalid
I have googled a lot about this and in every result I found the person was using the subprocess module, which I'm not, which makes it even more difficult.
The code is for downloading images. It does download the first one successfully, then it crashes. Anyone know what's causing the error? I'm still a beginner.
you're passing your counter as a second parameter. In the end the function uses that as a filename to copy the downloaded data to.
By passing an integer you enable the "pass a lowlevel file descriptor" to open:
file is either a string or bytes object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped.
But the file descriptor doesn't exist (fortunately!)
Let's reproduce the issue here:
>>> open(4,"rb")
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
OSError: [Errno 9] Bad file descriptor
the fix is obviously:
Download(link, file_name)
(I'd suggest that you rename your filenaame counter for something more meaningful, it avoids mistakes like that)
At this line,
urllib.request.urlretrieve(url, file_name)
here file_name should be a string so before calling Download(link, filenaame) make integer filenaame to string try.
Download(link, str(filenaame))
Sample working for downloading an image
def download_img(url):
name = random.randrange(100,1000)
full_name = str(name)+ ".jpg"
urllib.request.urlretrieve(url, full_name)
download_img("http://images.freeimages.com/images/small-previews/25d/eagle-1523807.jpg")

Python fails writing to a file citing 'FileNotFoundError'

Why does the following interaction fail? (python 3.6.1)
>>> with open('an_image.png', 'rb') as f, open('~/Desktop/an_image.png', 'wb') as g:
... g.write(f.read())
...
Traceback (most recent call last): File "<stdin>", line 1, in
<module> FileNotFoundError: [Errno 2] No such file or directory:
'~/Desktop/an_image.png'
>>>
Isn't the 'w' mode supposed to create the file if it doesn't exist?
As Dilettant said, remove the ~. You can specify the absolute path manually, or use os.path.expanduser:
import os
desktop_img = os.path.expanduser('~/Desktop/an_image.png')
# desktop_img will now be /home/username/Desktop/an_image.png
with open('an_image.png', 'rb') as f, open(desktop_img, 'wb') as g:
g.write(f.read())

python os.remove can not access the file

Why am I getting
Traceback (most recent call last): File "C:\temp\py\tesst.py", line 8, in <module>
os.remove( PATH ) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
'C:\temp\py\test.txt'
import os
PATH = r'C:\temp\py\test.txt'
f = open ( PATH,'w')
f.write('test\n')
f.close;
os.remove( PATH )
Am I missing something?
You are calling f.close instead of f.close(). It would be better to open the file contextually so it will be closed automatically.
import os
PATH = r'C:\temp\py\test.txt'
with open(PATH, 'wb') as f:
f.write('test\n')
os.remove(PATH)

Python, unpacking a .jar file, doesnt work

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

Categories