Fix ResourceWarning: unclosed file (#681)

Signed-off-by: Mickaël Schoentgen <contact@tiger-222.fr>
This commit is contained in:
Mickaël Schoentgen
2019-01-08 09:59:23 +01:00
committed by Libin Yang
parent 3dc50529ca
commit 2d70e9f747
10 changed files with 104 additions and 110 deletions

View File

@@ -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')

View File

@@ -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()