我有以下代码来检查我的 s3 存储桶
中是否存在文件。我给程序一个目录,然后尝试上传该目录中的所有文件。但是我不知道为什么我一直遇到 OSError: [Errno 2] No such file or directory: 'foo.txt'
错误。这是我的代码
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
我检查了我的堆栈数据,我的存储桶具有正确的存储桶名称,传输文件为 /Users/merlin/Desktop/test_folder/foo.txt
,源大小为 141
(正确尺寸)。
我将以下几行代码放入一个新文件中进行测试,它运行得很好。
import os
transfer_file = '/Users/merlin/Desktop/test_folder/foo.txt'
local_time = os.path.getmtime(os.path.basename(transfer_file))
print local_time
最佳答案
os.path.basename(transfer_file)
将仅返回路径中的文件名。从任何 os.path 方法的角度来看,这是一个相对路径。除非您从 "foo.txt"
所在的目录运行程序,否则 os.path.getmtime
肯定会失败。您需要将绝对路径传递给“foo.txt”
。您可能只想使用os.path.getmtime(transfer_file)
。
关于python - 操作系统错误: no Such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29151208/