1.python和C/C++的位置索引都是从0开始的,matlab的位置索引是从1开始的

2.open可以以只读'r'和只写'w'的方式打开和创建文件

如ftxt=open('list.txt','w')   以只写的方式打开或创建list.txt文件

3.os.listdir()获取特定文件夹下的所有文件名

allfiles=os.listdir('.')获取当前文件夹下的文件

allfiles=os.listdir(rootdir)获取rootdir文件夹下的文件

4.用循环遍历某种特定格式的文件

for i in allfiles:

    if os.path.splitext(i)[1]=='.jpg':

        abspath=oa.path.abspath(i)

        ftext.write(abspath+'\n')

ftxt.close()

其中,splitext可以将文件名与扩展名分离开来

          abspath用于获取文件的路径

程序:

python读取文件常用的操作1

输出:产生一个list.txt文件,里面的内容为

python读取文件常用的操作1

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-05
  • 2021-06-24
  • 2021-08-27
  • 2022-12-23
猜你喜欢
  • 2021-12-31
  • 2021-08-12
  • 2021-11-19
  • 2022-12-23
  • 2021-12-03
  • 2021-07-27
  • 2022-12-23
相关资源
相似解决方案