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]]

Python Base Four的更多相关文章

  1. Python Base of Scientific Stack(Python基础之科学栈)

    Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...

  2. Python Base One

    //this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...

  3. Python Base Five

    // 8 day(2016/8/11) 38. In python , it is oop. class Baskball:         def setName(self, name):      ...

  4. Python Base Three

    //sixth day to study python(2016/8/7) 32. In python , there are have an special type dictionary , it ...

  5. Python Base Two

    //fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...

  6. 2019-04-18 Python Base 1

    C:\Users\Jeffery1u>python Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64 ...

  7. python base 64

    python中base64编码与解码   引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...

  8. Python Base HTTP Server

    import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...

  9. 基于Python+协程+多进程的通用弱密码扫描器

    听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...

随机推荐

  1. python基础一 day15 复习

    迭代器和生成器迭代器 可迭代协议 —— 含有iter方法的都是可迭代的 迭代器协议 —— 含有next和iter的都是迭代器 特点 节省内存空间 方便逐个取值,一个迭代器只能取一次.生成器 —— 迭代 ...

  2. Nuget使用备忘

    菜单:工具-库程序包管理器-管理解决方案的NuGet程序包,搜索,下载,安装 或者 工具-库程序包管理器-程序包管理器控制台,输入PM命令,如: install-package log4net 如果不 ...

  3. linux下libnet编程 亲自测试可用

    linux下libnet编程 亲自测试可用 亲自测试  如果build包的时候 只要把类型改了 就能改成相应的协议. 0x0800 ip 0x0806 arp 0x86DD    IPv6 0x86e ...

  4. linux更新git

    在CentOS中使用yum install git安装的git是1.7版本的,所以需要更新1.9以及更高版本的git. 安装方法如下: 1.安装依赖的包: yum -y install curl-de ...

  5. 吐槽在cocos2dx游戏接入腾讯信鸽的坑

    腾讯信鸽是用来在后台推送消息给移动应用客户端使用,接入方法很简单,在信鸽官网注册个账号 http://xg.qq.com/xg,然后注册一个应用,在后台页面获得ACCESS ID, ACCESS KE ...

  6. IIS7.0/8.0的错误HTTP Error 500.19 - Internal Server Error ,错误代码为0x80070021

    最近在部署项目的时候,总是出现了这个问题. 大概原因为IIS7.0的安全设定相比前版本有很大的变更.IIS7.0的安全设置文件在%windir%\system32\inetsrv \config\ap ...

  7. Linux虚拟机里用X11协议打开图形界面的eclipse

    1.下载工具包 XLaunch(安装到win)https://xming.en.softonic.com/ Eclipse IDE for C/C++ Developers(虚拟机里解压到 /data ...

  8. biological clock--class

    '''this application aimed to cauculate people's biological block about emotional(28), energy(23),int ...

  9. 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)

    老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...

  10. BZOJ 2721: [Violet 5]樱花

    (X-N)(Y-N)=N^2 #include<cstdio> using namespace std; const int mod=1e9+7; int n,cnt,isprime[10 ...