Skip to content

Commit e17f745

Browse files
DavidLiu123forrest.liu
andauthored
add example of fs (#3)
* add example of fs * adjust the fs example directory structure --------- Co-authored-by: forrest.liu <forrest.liu@quectel.com>
1 parent f31fc37 commit e17f745

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import ql_fs
2+
3+
def main():
4+
# 递归式创建文件夹, 传入文件夹路径
5+
ql_fs.mkdirs("usr/a/b")
6+
7+
# 查看文件或文件夹是否存在
8+
ret = ql_fs.path_exists("/usr/a/b")
9+
if ret:
10+
print("make dir success")
11+
else:
12+
print("make dir fail")
13+
return
14+
15+
# 创建文件或者更新文件数据
16+
data = {"test":1}
17+
ql_fs.touch("/usr/a/b/config.json", data)
18+
19+
# 查看文件或文件夹是否存在
20+
ret = ql_fs.path_exists("/usr/a/b/config.json")
21+
if ret:
22+
print("create file success")
23+
else:
24+
print("create file fail")
25+
return
26+
27+
# 读取json文件
28+
data = ql_fs.read_json("/usr/a/b/config.json")
29+
print("config json read content:{}".format(data))
30+
data = ql_fs.read_json("/usr/system_config.json")
31+
len = ql_fs.path_getsize('usr/system_config.json')
32+
print("system_config json length:{}".format(len))
33+
print("system_config json read content:{}".format(data))
34+
35+
# 文件拷贝
36+
ql_fs.file_copy("/usr/a/b/config.json", "usr/system_config.json")
37+
data = ql_fs.read_json("/usr/a/b/config.json")
38+
print("copy json read content:{}".format(data))
39+
40+
# 获取文件所在文件夹路径
41+
ret = ql_fs.path_dirname("/usr/a/b/config.json")
42+
print("path of the file:{}".format(ret))
43+
44+
# 删除文件夹和其下的文件
45+
ql_fs.rmdirs("usr/a")
46+
47+
# 查看文件或文件夹是否存在
48+
ret = ql_fs.path_exists("/usr/a/b/config.json")
49+
if ret:
50+
print("remove file fail, test fail")
51+
52+
ret = ql_fs.path_exists("/usr/a/b")
53+
if ret:
54+
print("remove dir2 fail, test fail")
55+
56+
ret = ql_fs.path_exists("/usr/a")
57+
if ret:
58+
print("remove dir1 fail, test fail")
59+
60+
61+
if __name__ == "__main__":
62+
main()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 创建一个test.txt文件,注意路径在/usr下
2+
f = open("/usr/test.txt", "w+")
3+
i = f.write("hello world\n")
4+
i = i + f.write("hello quecpython\n")
5+
print("write length{}".format(i))
6+
f.seek(0)
7+
str = f.read(10)
8+
print("read content:{}".format(str))
9+
f.seek(0)
10+
str = f.readline()
11+
print("read line content:{}".format(str))
12+
f.seek(0)
13+
str = f.readlines()
14+
print("read all lines content:{}".format(str))
15+
f.close()
16+
print("test end")
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import uos
2+
3+
def main():
4+
# 创建文件 此处并没有对文件进行写入操作
5+
f = open("/usr/uos_test","w")
6+
f.close()
7+
del f
8+
9+
# 查看文件是否存在
10+
t = uos.listdir("/usr")
11+
print("usr files:{}".format(t))
12+
13+
if "uos_test" not in t:
14+
print("file not exist test fail")
15+
return
16+
17+
# 查看文件状态
18+
a = uos.stat("/usr/uos_test")
19+
print("file size:{}bytes".format(a[6]))
20+
21+
# 重命名文件
22+
uos.rename("/usr/uos_test", "/usr/uos_test_new")
23+
t = uos.listdir("/usr")
24+
print("test file renamed, usr files:{}".format(t))
25+
26+
if "uos_test_new" not in t:
27+
print("renamed file not exist test fail")
28+
return
29+
30+
# 删除文件
31+
uos.remove("/usr/uos_test_new")
32+
t = uos.listdir("/usr")
33+
print("remove test file, usr files:{}".format(t))
34+
35+
if "uos_test_new" in t:
36+
print("remove file fail, test fail")
37+
return
38+
39+
# 目录操作
40+
t = uos.getcwd()
41+
print("current path:{}".format(t))
42+
uos.chdir("/usr")
43+
t = uos.getcwd()
44+
print("current path:{}".format(t))
45+
if "/usr" != t:
46+
print("dir change fail")
47+
return
48+
49+
uos.mkdir("testdir")
50+
t = uos.listdir("/usr")
51+
print("make dir, usr files:{}".format(t))
52+
53+
if "testdir" not in t:
54+
print("make dir fail")
55+
return
56+
57+
uos.rmdir("testdir")
58+
t = uos.listdir("/usr")
59+
print("remove test dir, usr files:{}".format(t))
60+
61+
if "testdir" in t:
62+
print("remove dir fail")
63+
return
64+
65+
if __name__ == "__main__":
66+
main()

0 commit comments

Comments
 (0)