PYTHON   18

main.py

Guest on 12th August 2021 01:17:48 PM

  1. from machine import SD
  2. import os
  3.  
  4. sd = SD()
  5.  
  6. # mount SD if not mounted already
  7. try:
  8.     os.mount(sd, '/sd')
  9. except OSError:
  10.     print("SD card already mounted.")
  11.     pass
  12.  
  13. # create 'log' folder if does not exist
  14. try:
  15.     os.listdir('/sd/log')
  16.     print('/sd/log file already exists.')
  17. except OSError:
  18.     print('/sd/log file does not exist. Creating it ...')
  19.     os.mkdir('/sd/log')
  20.  
  21. file_path = '/sd/log'
  22. name = '/my_second_file.log'
  23.  
  24. # Writing
  25. with open(file_path + name, 'w') as f:
  26.     f.write('Testing write operations in a file.')
  27.  
  28. # Reading
  29. with open(file_path + name, 'r') as f:
  30.     print(f.readall())

Raw Paste


Login or Register to edit or fork this paste. It's free.