Skip to content

Commit c7df9c6

Browse files
slzatzdpgeorge
authored andcommitted
stmhal: Add os.rename function.
1 parent f601390 commit c7df9c6

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

docs/library/os.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ Functions
4646

4747
Remove a directory.
4848

49+
.. function:: rename(old_path, new_path)
50+
51+
Rename a file.
52+
4953
.. function:: stat(path)
5054

5155
Get the status of a file or directory.

stmhal/moduos.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ STATIC mp_obj_t os_remove(mp_obj_t path_o) {
239239
}
240240
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove);
241241

242+
/// \function rename(old_path, new_path)
243+
/// Rename a file
244+
STATIC mp_obj_t os_rename(mp_obj_t path_in, mp_obj_t path_out) {
245+
const char *old_path = mp_obj_str_get_str(path_in);
246+
const char *new_path = mp_obj_str_get_str(path_out);
247+
FRESULT res = f_rename(old_path, new_path);
248+
switch (res) {
249+
case FR_OK:
250+
return mp_const_none;
251+
default:
252+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error renaming file '%s' to '%s'", old_path, new_path));
253+
}
254+
255+
}
256+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename);
257+
242258
/// \function rmdir(path)
243259
/// Remove a directory.
244260
STATIC mp_obj_t os_rmdir(mp_obj_t path_o) {
@@ -368,6 +384,7 @@ STATIC const mp_map_elem_t os_module_globals_table[] = {
368384
{ MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&os_listdir_obj },
369385
{ MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj },
370386
{ MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj },
387+
{ MP_OBJ_NEW_QSTR(MP_QSTR_rename),(mp_obj_t)&os_rename_obj},
371388
{ MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_rmdir_obj },
372389
{ MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj },
373390
{ MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove

stmhal/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ Q(chdir)
379379
Q(getcwd)
380380
Q(listdir)
381381
Q(mkdir)
382+
Q(rename)
382383
Q(remove)
383384
Q(rmdir)
384385
Q(unlink)

0 commit comments

Comments
 (0)