• Python Base Four


    35. In python, file operation syntax is similar to c.

          open(file,'r',……) //the first parameters is necessary, other is optional ,the second parameters is 'r' by default

          if you want to open a file, you can use:

          f = open('//Users//wyg_mac//Desktop//Account.txt') //if you don't know the path ,you can drag the file to console

          if you want to close file ,you can use:

          f.close()

          next now that we know how to open file and close file ,but how to read file content.

          you can use

          f.read(size = -1) //read size buffer, if size not define or  minus ,read next buffer , and return as string

          f.readline() //open as write mode, if file exist, append at end

          if you want to write content to file ,how to achieve it.

          f.wirte(str) // put str write file

          f.wirtelines(seq) //

          f.tell() // return current position

          f.seek(offset,from) //from( if is 0, start position,1 current position 2 end position) offset

          such as if i want to create a file demo.txt on desktop and write content whth 'write code change world'

          f = open(r'/users/wyg_mac/desktop/demo.txt','w')

          f.wirte('write code change world')

          f.close()

          there are something we need to notice:

          if a file not exist , as follow ,it will have error:

          f = open(r'/users/wyg_mac/desktop/a.txt')  -> error

          if file not exist ,you use follow it will be right:

          f = open(r'/users/wyg_mac/desktop/a.txt') -> right

    //seventh day to study python

    36. In python , os module is base and important. 

          import os

          os.getcwd()  //get current file path

          -> '/users/wyg_mac/documents'

          os.chdir('/users/wyg_mac/desktop')  //change file path

          os.getcwd()

          -> '/users/wyg_mac/desktop

          os.listdir('/users/wyg_mac/desktop')  //list file dir

          -> ['demo.txt', '.DS_Store',]

          os.mkdir('/users/wyg_mac/desktop/a') 

          -> create a file named a

          os.mkdir('/users/wyg_mac/desktop/a/b')

          -> if file a exist ,create file named b in file a, if a not exist ,will have an error

          os.makedirs('/users/wyg_mac/desktop/m/n/o')

          -> create file m on desktop , m include n , n include o

          if file m/n/o ,o include ttt.txt

          os.remove('/users/wyg_mac/desktop/m/m/o/ttt.txt')

          -> remove ttt.txt according to path 

          os.rmdir('/users/wyg_mac/desktop/m/n/o')

          -> remove o file , if o contain content , error , if not ,remove it.

          os.removedirs('path')

          ->

          os.rename('/users/wyg_mac/desktop/a','/users/wyg_mac/desktop/aaa')

          -> change file name

          os.system(command)

          -> command is shell command, such as os.system('cmd')

          os.curdir  //current

          -> .

          os.pardir //

          -> ..

          such as:

          os.listdir(os.curdir' == os.listdir('.')

          os.sep   //current os path sep

          -> /

          os.name //current os name

         -> 'posix'

    37. os.path

          os.path.bastname('/users/wyg_mac')

          -> wyg_name

          os.path.dirname('/users/wyg_mac/desktop')

          -> users/wyg_mac

         os.path.join('/users','wyg_mac','desktop')

         -> '/users/wyg_mac/desktop'

         os.path.split('/users/wyg_mac/desktop/m/n')

         -> ('/users/wyg_mac/desktop/m','n')

         os.path.splitext('/users/wyg_mac/desktop/m')

         -> ('/users/wyg_mac/desktop/m','')

         os.path.splitext('/users/wyg_mac/desktop/m.txt')

         -> ('/uses/wyg_mac/desktop/m','.txt')

         os.path.getsize('/users/wyg_mac/desktop/m.txt')

         -> 35

         os.path.getctime()

         os.path.getmtime()

         os.path.getatime()

         os.path.mtime('/users/wyg_mac/desktop/m')

         -> 1470748934.0

         if you don not understand it ,you can use:

         import time

         time.localtime(os.path.getmtime('/users/wyg_mac/desktop/m'))

         -> time.struct_time(tm_year=2016, tm_mon=8, tm_mday=9, tm_hour=21, tm_min=22, tm_sec=14, tm_wday=1, tm_yday=222, tm_isdst=0

         os.path.exists('/users/wyg_mac/desktop/m')

         -> True

         os.path.isabs()  //if if abs path

         os.path.isfile()

         os.path.isdir()

         os.path.ismount()

         os.path.islink()

         os.path.samefile(,)

    38. pickle module ,you can store data to file and read it

          import pickle

          my_list = [123, 3.14, 'roy' [1, 3]]

          pickle_file = open('my_list.txt', 'wb')

          pickle.dump(my_list, pickle_file)

          pickle_file.close()

          pickle_file = open('my_list.txt','rb')

          my_list2 = pickle.load(pickle_file)

          print(my_list2)

          -> [123, 3.14, 'roy', [1, 3]]

     

  • 相关阅读:
    ASP.NET常见命名空间及其功能描述
    C#中的里氏替换原则
    Java中的split函数的用法
    shader之旅-7-平面阴影(planar shadow)
    OpenGL三角形的双面不同颜色的绘制
    MATLAB squeeze 函数
    matlab 常用函数汇总
    第一个OpenGL程序
    Github 留言系统
    Paging 简单自由的分页生成器
  • 原文地址:https://www.cnblogs.com/wangyaoguo/p/5747508.html
Copyright © 2020-2023  润新知