Page MenuHomec4science

Copy_Vehicules.py
No OneTemporary

File Metadata

Created
Fri, Jan 31, 01:17

Copy_Vehicules.py

from functions import *
import os
import os
from ftplib import FTP
def access_server(local_directory, server_address, server_port, username, password):
try:
# Connect to the server
ftp = FTP()
ftp.connect(server_address, server_port)
print('Connected to the FTP server.')
# Login with credentials
ftp.login(username, password)
print('Logged in successfully.')
# Perform operations on the server
# For example, list files in the current directory
ftp.retrlines('LIST')
# copy files to local directory
Site_list = ftp.nlst()
# Copy each file to the local repository
for Site in Site_list:
if "Site" in Site:
Folder_list = ftp.nlst(Site)
for Folder in Folder_list:
if ("Vehicules" in Folder):
file_list = ftp.nlst(Folder + "/Data")
# Only select file containing ".csv"
file_list = [file for file in file_list if ".csv" in file]
for file_name in file_list:
# Create necessary directories recursively
local_path = os.path.join(local_directory, Folder)
create_directory(local_path)
local_file = file_name.replace("Data/", "")
# Check if the file already exists in the local repository
if os.path.exists(os.path.join(local_directory, local_file)):
pass
else:
# Copy the file
file_renamed = os.path.join(local_directory, local_file)
with open(file_renamed, 'wb') as file:
ftp.retrbinary('RETR ' + file_name, file.write)
print('Copied', file_name)
# Close the FTP connection
ftp.quit()
print('Disconnected from the FTP server.')
except ConnectionRefusedError:
print('Connection refused. Make sure the FTP server is running.')
except Exception as e:
print('An error occurred:', str(e))
def create_directory(path):
# Create necessary directories recursively
os.makedirs(path, exist_ok=True)
def main():
local_directory="data/Brute/"
# Server information
server_address = '80.74.143.13'
server_port = 21
username = 'ch.epfl.mobility'
#Adapt the file or set the pwd yourself
with open("C:/Users/cruz/Pwd.yaml", 'r') as stream:
try:
pwd = yaml.safe_load(stream)
password = pwd["Radar_server"]
except yaml.YAMLError as exc:
print(exc)
access_server(local_directory, server_address, server_port, username, password)
if __name__ == '__main__':
main()
exit()

Event Timeline