mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Fix ResourceWarning: unclosed file (#681)
Signed-off-by: Mickaël Schoentgen <contact@tiger-222.fr>
This commit is contained in:
committed by
Libin Yang
parent
3dc50529ca
commit
2d70e9f747
@@ -17,13 +17,12 @@ while True:
|
||||
print('Server received', repr(data))
|
||||
|
||||
filename = 'mytext.txt'
|
||||
f = open(filename, 'rb')
|
||||
in_data = f.read(1024)
|
||||
while (in_data):
|
||||
conn.send(in_data)
|
||||
print('Sent ', repr(in_data))
|
||||
in_data = f.read(1024)
|
||||
f.close()
|
||||
with open(filename, 'rb') as f:
|
||||
in_data = f.read(1024)
|
||||
while in_data:
|
||||
conn.send(in_data)
|
||||
print('Sent ', repr(in_data))
|
||||
in_data = f.read(1024)
|
||||
|
||||
print('Done sending')
|
||||
conn.send('Thank you for connecting')
|
||||
|
||||
@@ -20,10 +20,9 @@ ftp.cwd('/Enter the directory here/')
|
||||
|
||||
def ReceiveFile():
|
||||
FileName = 'example.txt' """ Enter the location of the file """
|
||||
LocalFile = open(FileName, 'wb')
|
||||
ftp.retrbinary('RETR ' + FileName, LocalFile.write, 1024)
|
||||
with open(FileName, 'wb') as LocalFile:
|
||||
ftp.retrbinary('RETR ' + FileName, LocalFile.write, 1024)
|
||||
ftp.quit()
|
||||
LocalFile.close()
|
||||
|
||||
"""
|
||||
The file which will be sent via the FTP server
|
||||
@@ -32,5 +31,6 @@ def ReceiveFile():
|
||||
|
||||
def SendFile():
|
||||
FileName = 'example.txt' """ Enter the name of the file """
|
||||
ftp.storbinary('STOR ' + FileName, open(FileName, 'rb'))
|
||||
with open(FileName, 'rb') as LocalFile:
|
||||
ftp.storbinary('STOR ' + FileName, LocalFile)
|
||||
ftp.quit()
|
||||
|
||||
Reference in New Issue
Block a user