Write to SD Card on EXP Brain using Python

I saw that the EXP Brain has a microSD slot. I have purchased some cards but am unsure how to call the necessary function to write to the microSD card in Python. The EXP brain requires me to use the vexcode EXP software as opposed to Vexcode Pro.

@mhilton

Thanks for reaching out.

Yes, you can use VEXCode for EXP with python to access the SD card for file IO.
You can use the standard file IO calls in python to read and write files to the SD card.

Here is a simple example for writing a CSV file and then reading it back and parsing the data:

# Library imports
from vex import *

file = 'myfile.csv'
counter = 0

try:

    # write some CSV data
    # "w" will overwrite if the file already exists
    with open(file, "w") as f:
        counter = 0
        while counter < 10:
            bytesWritten = f.write('{0},'.format(counter))
            counter = counter + 1

    # read some CSV data
    data = []
    with open(file, "r") as f:
        for line in f.readlines():
            print("Line from File:")
            print(line)
            data.append(line.split(","))

    print()

    # print data to console
    print('Data from ' + file)
    print(data) 

except OSError:
    print("File access error.\n")
    print("Make sure SD Card is inserted \n")
    print("and formated as FAT32.\n")

I hope that will get you going. If you have any more questions just let us know.

Just FYI we are working on a KB article that will cover this in a bit more depth. We will post a link here once it is published.

6 Likes

Thank you so much! I am looking forward to using this with my students.

@levipope
I am getting an error code when I use this with the VEXcode EXP .

Traceback (most recent call last):
File “userpy”, line 32, in
OS Error: 5

Looks like the code is having trouble accessing the SD card. Can you make sure the SD cards are formatted as FAT32.

If you can send details on your SD card that may help trouble shoot.

2 Likes

Found out the error. Needed to format the sd card to FAT32. exFAT does not work.

2 Likes