os
– Basic Operating System¶
This module implements the corresponding Cpython module CPython a subset of modules, as follows. Refers to CPython document for details: os
The UOS module contains file system access and installation, terminal redirection and replication function of uname
and urandom
functions.
Main Functions¶
-
os.
uname
()¶
Returns a tuple (possibly a named tuple), it contains information about the underlying and / or its operating system. Tuples have five fields, each in the following order:
sysname
– Name of underlying systemnodename
– Network name (same as sysname)release
– Version of underlying systemversion
– MicroPython version and build datemachine
– Identifier of the underlying hardware(eg board, CPU)
Example:
>>> import os
>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.9.1', version='v1.9.1-224-g83d3f3f-dirty on 2017-12-12', machine='ESP32 module with ESP32')
-
os.
urandom
(n)¶
Returns a byte object with N random bytes. It is generated by hardware random number generator.
Example:
>>> os.urandom(20)
b'f\x92\x85t28\xa1\xf0\xaf3\xf5\xd9\xcdx\xc3\n\xedm\xf8\xb7'
File system access¶
-
os.
getcwd
()¶
Access the current directory.
-
os.
chdir
(path)¶
Change the current directory.
Example:
>>> os.getcwd()
'/'
>>> os.chdir("./lib")
>>> os.getcwd()
'/lib'
>>> os.chdir("..")
>>> os.getcwd()
'/'
-
os.
ilistdir
([dir])¶
This function returns an iterator and generates a tuple corresponding to the entries in the listed directory. No parameters, it lists the current directory, otherwise it lists the directory given by dir.
The 3-tuples have the form (name, type, inode):
- name is a string (bytes if dir is a byte object) and is the name of the entry
- type is an integer that specifies the type of the entry. The directory is 0x4000 and the general file is 0x8000;
- inode is an integer corresponding to the file inode, which can be 0 for a file system without this concept.
At present, the meaning of the article has not been defined.
-
os.
listdir
([dir])¶
If there are no parameters, list the current directory. Otherwise, the given directory is listed.
- Example::
>>> os.listdir() ['boot.py', 'lib'] >>> os.listdir("./lib") ['test.py']
-
os.
mkdir
(path)¶
Create directory, path is the way to create directory.
example:
>>> os.listdir()
['boot.py']
>>> path = "./lib"
>>> os.mkdir(path)
>>> os.listdir()
['boot.py', 'lib']
-
os.
rmdir
(path)¶
Delete directory.
Example:
>>> os.listdir()
['boot.py', 'lib']
>>> os.rmdir("./lib")
>>> os.listdir()
['boot.py']
-
os.
remove
(path)¶
Delete file.
Example:
>>> os.listdir("./lib")
['test.py']
>>> os.remove("./lib/test.py")
>>> os.listdir("./lib")
[]
-
os.
rename
(old_path, new_path)¶
Rename file.
Example:
>>> os.listdir(os.getcwd())
['test.py']
>>> os.rename("test.py", "mytest.py")
>>> os.listdir(os.getcwd())
['mytest.py']
-
os.
stat
(path)¶
Obtain the status of a file or directory.
Example:
>>> os.stat("./lib")
(16384, 0, 0, 0, 0, 0, 0, 0, 0, 0)
>>> os.stat("./lib/test.py")
(32768, 0, 0, 0, 0, 0, 1, 0, 0, 0)
-
os.
statvfs
(path)¶
Obtain the status of the file system.
Returns a tuple containing file system information in the following order:
f_bsize
– File system block sizef_frsize
– Fragment sizef_blocks
– f_frsize unit size of FSf_bfree
– free blocks amountf_bavail
– number of free blocks for unpriviliged usersf_files
– inodes amountf_ffree
– number of free inodesf_favail
– number of free inodes for unpriviliged usersf_flag
– mount flagsf_namemax
– maximum filename length
Related node parameters information: f_files
,f_ffree
,f_avail
和 f_flags
Parameter may return 0.
-
os.
sync
()¶
Synchronize all file systems.
Terminal redirection and replication¶
-
os.
dupterm
(stream_object, index=0)¶
Copy or switch given similar stream
MicroPython terminal on object(REPL). The stream_object arameter must be a local stream object, or export from uio.IOBase
and implement readinto()
and write()
method. The stream should be in non blocking mode if there is no data available for reading, readinto()
should return None
。
When this function is called, all terminal output is repeated on this stream, and any input available on the stream is passed to the terminal input.
Which replication time slot the index parameter should be is set as a non negative integer, and specified. A given port can implement multiple slots (slot 0 will always be available), and in this case, terminal inputs and outputs are copied on all the slots set.
If None
is passed as stream_object, the copy will be cancelled on the slot given by the index.
File system installation¶
Provides virtual file system (VFS) and the ability to install multiple “real” file systems in this VFS. File system objects can be installed in the root of VFS or in a subdirectory of the root.
This allows Python programs to see the dynamic and flexible configuration of the file system. It provides the functions of mount()
and umount()
, as well as various file systems that may be represented by VFS classes.
-
os.
mount
(fsobj, mount_point, *, readonly)¶ -
File system objects `fsobj` mounted to the location in the VFS specified by the mount_point string.
-
Fsobj can be a VFS object with ``mount()`` method or block device. If it is a block device, the file system type is automatically detected (an exception is thrown if the file system is not recognized).
-
`mount_point` It can be in the root directory '/'mount `fsobj`, also '/<name>' ou can mount it in a subdirectory at the root.
If readonly is True The file system is mounted read-only.
During mounting, call this method on the file system object for mount()
.
OSError(EPERM)
if mount_point had installed, will cause.
-
os.
umount
(mount_point)¶ Unmount file system. mount_point it can be a string that names the installation location, it can also be a previously installed file system object. During the unmount process, the umount() call this method on a file system object.
OSError(EINVAL)
in case can’t locate mount_point ,will cause.