Reputation: 2567
I have the following piece of code to check if a file exists in my s3 bucket
. I give the program a directory and I am trying to upload all the files in that directory. However I have no idea why I keep running into the OSError: [Errno 2] No such file or directory: 'foo.txt'
error. This is my code
def s3_has_uptodate_file(bucket, transfer_file, source_size):
bucket = Bucket(conn, default_bucket)
s3_key = bucket.get_key(os.path.basename(transfer_file))
if s3_key:
s3_size = s3_key.size
local_size = source_size
s3_time = rfc822.mktime_tz(rfc822.parsedate_tz(s3_key.last_modified))
local_time = os.path.getmtime(os.path.basename(transfer_file)) #fails here
return s3_size == local_size and s3_time >= local_time
return False
I check my stackdata and my bucket has the right bucket name, transfer file is /Users/merlin/Desktop/test_folder/foo.txt
and source size is 141
(correct size).
I put the following lines of code into a new file to test it out and it runs perfectly.
import os
transfer_file = '/Users/merlin/Desktop/test_folder/foo.txt'
local_time = os.path.getmtime(os.path.basename(transfer_file))
print local_time
Upvotes: 1
Views: 1733
Reputation: 9075
os.path.basename(transfer_file)
will return just the file name off a path. This is a relative path from the prospective of any os.path
methods. Unless you are running the program from the directory in which "foo.txt"
lives in, os.path.getmtime
will most certainly fail. You need to pass the absolute path to "foo.txt"
. You probably just want to use os.path.getmtime(transfer_file)
.
Upvotes: 2