How do you move files inside sdcard with micropython?
How do you move files inside sdcard with micropython?
Hi all, I'm having troubles with my micropython board where I have several files(data0, data1, data2 etc...) on my /SD card directory where I want to move them to another directory, let's say: /SD/storage.
And as new data are being logged to my micropython which is equipped with sensors at the same time, I want the process to be automated.
How do I call upon the file and move them to another directory? I really appreciate any help, thanks!
And as new data are being logged to my micropython which is equipped with sensors at the same time, I want the process to be automated.
How do I call upon the file and move them to another directory? I really appreciate any help, thanks!
Re: How do you move files inside sdcard with micropython?
You can use uos.mkdir(dir) to create a directory, like uos.mkdir("storage"), and use uos.rename() to move files, like:
uos.rename("/SD/data0", "/SD/storage/data0").
uos.rename("/SD/data0", "/SD/storage/data0").
Re: How do you move files inside sdcard with micropython?
Thanks! Another question, how do you send those text files to an FTP server instead of sending rawData over?Roberthh wrote:You can use uos.mkdir(dir) to create a directory, like uos.mkdir("storage"), and use uos.rename() to move files, like:
uos.rename("/SD/data0", "/SD/storage/data0").
if SDtoFTP == 1:
f = open('/sd/data/data1', 'r')
f = f.readline()
SDtoFTP = 0
u2.write(f)
wdt.feed()
unfortunately, what I received over at my FTP was just blank text files. if I type u2.write(rawDatafromSensor), I would receive the data over at my FTP server but whenever it creates the second file, there is some data loss(about 10 secs worth of data) where the 3G module of my board tries to send a request over to the FTP server before sending the current data. What I'm trying to do now is to save the data to the SDcard and let the 3G module read the data from there before sending so that I would not have any data loss
Re: How do you move files inside sdcard with micropython?
Code: Select all
if SDtoFTP == 1:
f = open('/sd/data/data1', 'r')
f = f.readline()
Re: How do you move files inside sdcard with micropython?
Did you forget to close the file after writing to it?skemm wrote:unfortunately, what I received over at my FTP was just blank text files
Re: How do you move files inside sdcard with micropython?
Roberthh wrote:This looks strange, since you overwrite f, which is the file handle, with the content of the first line. It might work once, for the first line. If you use f.readlines(), then the full file is read at once.Code: Select all
if SDtoFTP == 1: f = open('/sd/data/data1', 'r') f = f.readline()
I did what you suggested and right now I have an error code [Errno 13] EACCES
Strange.... something related to File Permission issues
Re: How do you move files inside sdcard with micropython?
I would not reassign the file handle, and writte instead
But it is hard to tell what happens unless we see the full picture. And please tell also, which board and micropython build you are using.
Code: Select all
if SDtoFTP == 1:
f = open('/sd/data/data1', 'r')
data = f.readlines()
Re: How do you move files inside sdcard with micropython?
sorry for the very late reply, i've spent days debugging my program and I ran into this problem, hopefully the final issue in my project.Roberthh wrote:I would not reassign the file handle, and writte insteadBut it is hard to tell what happens unless we see the full picture. And please tell also, which board and micropython build you are using.Code: Select all
if SDtoFTP == 1: f = open('/sd/data/data1', 'r') data = f.readlines()
if SDtoFTP == 1:
f = open('/sd/data/data5.csv')
f = f.readline()
f.close()
u2.write(f)
SDtoFTP = 0
AttributeError: 'str' object has no attribute 'close'
What should I do if i want to write the data inside data to u2?
So sorry for many questions, I'm just starting getting accustomed to the python language
Re: How do you move files inside sdcard with micropython?
Again, you overwrite the file handle f with a result of readline, which then gets a str type, which indede has no attribute close. Change your code to:
Code: Select all
if SDtoFTP == 1:
f = open('/sd/data/data5.csv')
data = f.readline()
f.close()
u2.write(data)
SDtoFTP = 0
Re: How do you move files inside sdcard with micropython?
Roberthh wrote:Again, you overwrite the file handle f with a result of readline, which then gets a str type, which indede has no attribute close. Change your code to:Code: Select all
if SDtoFTP == 1: f = open('/sd/data/data5.csv') data = f.readline() f.close() u2.write(data) SDtoFTP = 0
thanks for the help, sir. You saved me lots of time.
unfortunately, it only reads the first line from data5.csv
is there any way i can read every line in my data5.csv?