Why the file I created is not created?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
destoriNAVY
Posts: 13
Joined: Wed Jan 20, 2021 7:19 am

Why the file I created is not created?

Post by destoriNAVY » Wed Jan 27, 2021 5:37 am

Code: Select all

with open("foo.txt", "w") as f:
    f.write("Life is too short, you need python")
As I expected, a text file should be created in PYBFLASH(E:), but it is not. How can I generate it?

IN PUTTY, when I run that code, it just showed to me '34'

but what I want is make 'text file'. What am I doing for solving this problem?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Why the file I created is not created?

Post by dhylands » Wed Jan 27, 2021 6:11 am

34 is the return value from f.write which is the number of bytes written to the file.

What makes you think that the file wasn't created?

To see what files are create, you can use :

Code: Select all

import os
os.listdir('/flash')

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why the file I created is not created?

Post by pythoncoder » Wed Jan 27, 2021 6:19 am

Is this an instance of this issue where the file may not be visible to Windows (E:)?
Peter Hinch
Index to my micropython libraries.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Why the file I created is not created?

Post by dhylands » Wed Jan 27, 2021 6:45 am

Ah yes - could very well be.

Files created on the pyboard will be "unknown" over MSC until the pyboard in unmounted and remounted, and then only if Windows didn't try to write some files.

If both windows and the pyboard try to write to an MSC filesystem, then the filesystem will likely get corrupted.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Why the file I created is not created?

Post by pythoncoder » Wed Jan 27, 2021 6:48 am

In my opinion (and experience) MSC mode is a recipe for corruption. I always disable it.
Peter Hinch
Index to my micropython libraries.

destoriNAVY
Posts: 13
Joined: Wed Jan 20, 2021 7:19 am

Re: Why the file I created is not created?

Post by destoriNAVY » Wed Jan 27, 2021 7:11 am

I cannot understand what you guys said. :(

Code: Select all

import os
with open("foo.txt", "w") as f:
    f.write("Life is too short, you need python")
os.listdir('')
I tried to the 'os.lisitdir('\flash')'
but "foo.txt" was not created.

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

Re: Why the file I created is not created?

Post by Roberthh » Wed Jan 27, 2021 7:56 am

try uos.listdir("/flash") (forward slash, not backslash)

destoriNAVY
Posts: 13
Joined: Wed Jan 20, 2021 7:19 am

Re: Why the file I created is not created?

Post by destoriNAVY » Wed Jan 27, 2021 8:24 am

foo.txt was not created in my virtual memory.

Code: Select all

with open("foo.txt", "w") as f:
    data=f.write("Life is too short, you need python")
print(data)
os.listdir('/flash')
when I used uos.lisdir, there was an error like this, 'NameError: name 'uos' isn't defined'

if that code successfully run, is text file created in PYBFLASH(E:)?

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

Re: Why the file I created is not created?

Post by Roberthh » Wed Jan 27, 2021 9:08 am

The module os ias an alias to uos with micropython. Which one to use depends on how you imported it. Both ways work. Uisng uos most of the time, I used that for my example.

destoriNAVY
Posts: 13
Joined: Wed Jan 20, 2021 7:19 am

Re: Why the file I created is not created?

Post by destoriNAVY » Wed Jan 27, 2021 10:14 am

I uploaded an image that is a screenshot of my monitor screen.

In puTTY, I found the 'foo.txt'

But, in File Explorer(PYBFLASH), I couldn't find 'foo.txt'

the final goal is By creating an image, the image(jpg) file need to be created in file explorer.

To do this, I need to create a jpg file in File Explorer.

How do I open a file by double clicking it?
Attachments
imaging1.png
imaging1.png (68.87 KiB) Viewed 4559 times

Post Reply