In the following code I generate a file, but nothing is written to the file:
I tried this frome the console, and everything worked.
For sure its because I am stupid. but I dont know where.
Is anyone willing to give me a helpful hint?
Thanx very much
ed
--------------------------------------------
# main.py -- put your code here!
import pyb
import os
pyb.usb_mode('CDC+HID') # dont know If this is really needed
w1 = pyb.ADC('X19')
w2 = pyb.ADC('X20')
pyb.LED(3).on() # just to see it working
log=open('weightlog2.txt','w')
for i in range(10):
x1=w1.read()
x2=w2.read()
summe=x1+x2 #this is the value I want to write to the file
print(x1,x2, summe)
t = pyb.millis() # for test purpose
print(t) # for testpurpose
log.write('{}\n'.format(t)) # doesnt work as intended
pyb.LED(2).off()
pyb.delay(800)
pyb.LED(2).on()
pyb.delay(200)
log.close()
pyb.usb_mode('CDC+MSC') # setting back to mass storage so that I can look at the file.
pyb.LED(3).off()
print("Done!")
not able to write into opened file
Re: not able to write into opened file
Sorry for not putting it into
But I am no longer allowed to delete or edit my old posting.
Code: Select all
Code
Re: not able to write into opened file
Try using a filename of '/flash/weightlog2.txt'
You can only modify pyb.usb_mode in boot.py. By the time main.py runs, its too late.
It also seems that you can't query pyb.usb_mode, so I created the following file, which you can use as boot.py, which writes the mode it used into /flash/usb_mode.py
I was then able to create a main.py that looks like this:
Instead of calling main() you could also put your real code inside say program.py and then do: and have program.py with:
You can only modify pyb.usb_mode in boot.py. By the time main.py runs, its too late.
It also seems that you can't query pyb.usb_mode, so I created the following file, which you can use as boot.py, which writes the mode it used into /flash/usb_mode.py
Code: Select all
import pyb
sw_pressed = False
sw = pyb.Switch()
led = pyb.LED(1) # Red
for i in range(20): # 20 per second
led.toggle()
if sw():
sw_pressed = True
break
pyb.delay(50)
led.off()
mode = 'CDC+HID'
if sw_pressed:
mode = 'CDC+MSC'
with open('/flash/usb_mode.py', 'w') as mode_file:
mode_file.write("mode = '{}'\n".format(mode))
pyb.usb_mode(mode)
Code: Select all
import usb_mode
import sys
import pyb
def main():
led = pyb.LED(3)
while True:
led.on()
pyb.delay(100)
led.off()
pyb.delay(100)
led.on()
pyb.delay(100)
led.off()
pyb.delay(700)
if usb_mode.mode == 'CDC+MSC':
print("CDC+MSC mode")
else:
main()
Code: Select all
import usb_mode
if usb_mode.mode == 'CDC+MSC':
print("CDC+MSC mode")
else:
import program
Code: Select all
import pyb
led = pyb.LED(3)
while True:
led.on()
pyb.delay(100)
led.off()
pyb.delay(100)
led.on()
pyb.delay(100)
led.off()
pyb.delay(700)