How do you move files inside sdcard with micropython?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

How do you move files inside sdcard with micropython?

Post by skemm » Tue Jul 25, 2017 2:11 am

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!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Tue Jul 25, 2017 5:31 am

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").

skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

Re: How do you move files inside sdcard with micropython?

Post by skemm » Tue Jul 25, 2017 9:51 am

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").
Thanks! Another question, how do you send those text files to an FTP server instead of sending rawData over?

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Tue Jul 25, 2017 10:00 am

Code: Select all

if SDtoFTP == 1:
	f = open('/sd/data/data1', 'r')
	f = f.readline()
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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: How do you move files inside sdcard with micropython?

Post by deshipu » Tue Jul 25, 2017 10:53 pm

skemm wrote:unfortunately, what I received over at my FTP was just blank text files
Did you forget to close the file after writing to it?

skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

Re: How do you move files inside sdcard with micropython?

Post by skemm » Wed Jul 26, 2017 4:21 am

Roberthh wrote:

Code: Select all

if SDtoFTP == 1:
	f = open('/sd/data/data1', 'r')
	f = f.readline()
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.

I did what you suggested and right now I have an error code [Errno 13] EACCES

Strange.... something related to File Permission issues

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Wed Jul 26, 2017 5:27 am

I would not reassign the file handle, and writte instead

Code: Select all

if SDtoFTP == 1:
   f = open('/sd/data/data1', 'r')
   data = f.readlines()
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.

skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

Re: How do you move files inside sdcard with micropython?

Post by skemm » Mon Jul 31, 2017 7:50 am

Roberthh wrote:I would not reassign the file handle, and writte instead

Code: Select all

if SDtoFTP == 1:
   f = open('/sd/data/data1', 'r')
   data = f.readlines()
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.
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.

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: How do you move files inside sdcard with micropython?

Post by Roberthh » Mon Jul 31, 2017 7:52 am

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

skemm
Posts: 10
Joined: Thu Jul 13, 2017 9:31 am

Re: How do you move files inside sdcard with micropython?

Post by skemm » Tue Aug 01, 2017 7:57 am

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. :D

unfortunately, it only reads the first line from data5.csv

is there any way i can read every line in my data5.csv?

Post Reply