gpt4 book ai didi

Python - 如何将今天在文件夹中创建的文件上传到 S3

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:47 27 4
gpt4 key购买 nike

我有一个名为 myfolder 的文件夹,其中包含多个文件,文件名如下,

ID001_2017-04-15.csv, ID002_2017-04-15.csv, ID001_2017-04-16.csv, ID002_2017-04-16.csv, 
ID001_2017-04-17.csv, ID002_2017-04-17.csv, ID001_2017-04-18.csv, ID002_2017-04-18.csv

文件名上的日期是文件的创建日期。例如,文件 ID001_2017-04-17.csv 创建于 2017-04-17。这是我将文件夹中的所有文件上传到 Amazon S3 的方法,

import boto3

def upload_files(path):
session = boto3.Session(
aws_access_key_id = 'this is my access key',
aws_secret_access_key = 'this is my secret key',
region_name = 'this is my region'
)
s3 = session.resource('s3')
bucket = s3.Bucket('this is my bucket')

for subdir, dirs, files in os.walk(path):
for file in files:
full_path = os.path.join(subdir, file)
with open(full_path, 'rb') as data:
bucket.put_object(Key = full_path[len(path) + 1:], Body = data)

if __name__ == "__main__":
upload_files('path to myfolder') ## Replace this with your folder directory

我的问题是我只能将今天创建的文件上传到 Amazon S3 吗?

最佳答案

这将检查文件是否是今天创建的:

import os.path
import datetime.datetime

# Create a datetime object for right now:
now = datetime.datetime.now()
# Create a datetime object for the file timestamp:
ctime = os.path.getctime('example.txt')
filetime = datetime.datetime.fromtimestamp(ctime)

# Check if they're the same day:
if filetime.year == now.year and filetime.month == now.month and filetime.day = now.day:
print('File was created today')

如果您在 for file in files: 循环中放入类似的内容,您应该能够隔离今天创建的文件。

关于Python - 如何将今天在文件夹中创建的文件上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43618801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com