@@ -72,14 +72,11 @@ Variables can be opened as readable or writable file objects which can be useful
7272when dealing with large amounts of data::
7373
7474 # Open the Store EDS variable as a file like object
75- infile = node.sdo[0x1021].open('r', encoding='ascii')
76- # Open a file for writing to
77- outfile = open('out.eds', 'w', encoding='ascii')
78- # Iteratively read lines from node and write to file
79- outfile.writelines(infile)
80- # Clean-up
81- infile.close()
82- outfile.close()
75+ with node.sdo[0x1021].open('r', encoding='ascii') as infile,
76+ open('out.eds', 'w', encoding='ascii') as outfile:
77+
78+ # Iteratively read lines from node and write to file
79+ outfile.writelines(infile)
8380
8481Most APIs accepting file objects should also be able to accept this.
8582
@@ -88,17 +85,16 @@ server supports it. This is done through the file object interface::
8885
8986 FIRMWARE_PATH = '/path/to/firmware.bin'
9087 FILESIZE = os.path.getsize(FIRMWARE_PATH)
91- infile = open(FIRMWARE_PATH, 'rb')
92- outfile = node.sdo['Firmware'].open('wb', size=FILESIZE, block_transfer=True)
93-
94- # Iteratively transfer data without having to read all into memory
95- while True:
96- data = infile.read(1024)
97- if not data:
98- break
99- outfile.write(data)
100- infile.close()
101- outfile.close()
88+
89+ with open(FIRMWARE_PATH, 'rb') as infile,
90+ node.sdo['Firmware'].open('wb', size=FILESIZE, block_transfer=True) as outfile:
91+
92+ # Iteratively transfer data without having to read all into memory
93+ while True:
94+ data = infile.read(1024)
95+ if not data:
96+ break
97+ outfile.write(data)
10298
10399.. warning ::
104100 Block transfer is still in experimental stage!
0 commit comments