Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F100468452
Copy_Vehicules.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, Jan 31, 01:17
Size
2 KB
Mime Type
text/x-python
Expires
Sun, Feb 2, 01:17 (2 d)
Engine
blob
Format
Raw Data
Handle
23969388
Attached To
R13222 Compteurs_radars_epfl
Copy_Vehicules.py
View Options
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
Log In to Comment