Page 1 of 2

Why the file I created is not created?

Posted: Wed Jan 27, 2021 5:37 am
by destoriNAVY

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?

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 6:11 am
by dhylands
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')

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 6:19 am
by pythoncoder
Is this an instance of this issue where the file may not be visible to Windows (E:)?

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 6:45 am
by dhylands
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.

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 6:48 am
by pythoncoder
In my opinion (and experience) MSC mode is a recipe for corruption. I always disable it.

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 7:11 am
by destoriNAVY
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.

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 7:56 am
by Roberthh
try uos.listdir("/flash") (forward slash, not backslash)

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 8:24 am
by destoriNAVY
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:)?

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 9:08 am
by Roberthh
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.

Re: Why the file I created is not created?

Posted: Wed Jan 27, 2021 10:14 am
by destoriNAVY
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?