Python: Tạo thư mục trên FTP server

import ftplib
import os
import datetime

# Define server, username, and password
server = 'ftp.example.com'
username = 'myusername'
password = 'mypassword'

# Connect to the server
ftp = ftplib.FTP(server)
ftp.login(username, password)

# Get the current date and time
now = datetime.datetime.now()
date_string = now.strftime("%Y-%m-%d")

# Create a folder with the current date as the name
folder_name = "/path/to/folder/" + date_string
ftp.mkd(folder_name)

# Change to the new folder
ftp.cwd(folder_name)

# Upload a file to the new folder
local_file_path = "/path/to/local/file.txt"
file_name = os.path.basename(local_file_path)
with open(local_file_path, 'rb') as f:
    ftp.storbinary('STOR ' + file_name, f)

# Close the connection
ftp.quit()

 

Bài viết cùng danh mục