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]]
Python Base Four的更多相关文章
- Python Base of Scientific Stack(Python基础之科学栈)
Python Base of Scientific Stack(Python基础之科学栈) 1. Python的科学栈(Scientific Stack) NumPy NumPy提供度多维数组对象,以 ...
- Python Base One
//this is my first day to study python, in order to review, every day i will make notes (2016/7/31) ...
- Python Base Five
// 8 day(2016/8/11) 38. In python , it is oop. class Baskball: def setName(self, name): ...
- Python Base Three
//sixth day to study python(2016/8/7) 32. In python , there are have an special type dictionary , it ...
- Python Base Two
//fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...
- 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 ...
- python base 64
python中base64编码与解码 引言: 在一些项目中,接口的报文是通过base64加密传输的,所以在进行接口自动化时,需要对所传的参数进行base64编码,对拿到的响应报文进行解码: Bas ...
- Python Base HTTP Server
import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...
- 基于Python+协程+多进程的通用弱密码扫描器
听说不想扯淡的程序猿,不是一只好猿.所以今天来扯扯淡,不贴代码,只讲设计思想. 0x00 起 - 初始设计 我们的目标是设计一枚通用的弱密码扫描器,基本功能是针对不同类型的弱密码,可方便的扩展,比如添 ...
随机推荐
- 第008课_第1个ARM落版程序及引申
form:第008课_第1个ARM裸板程序及引申 第001节_辅线1_硬件知识_LED原理图 当我们学习C语言的时候,我们会写个Hello程序.那当我们下ARM程序,也该有一个简单的程序引领我们入门, ...
- 学习笔记(四): Representation:Feature Engineering/Qualities of Good Features/Cleaning Data/Feature Sets
目录 Representation Feature Engineering Mapping Raw Data to Features Mapping numeric values Mapping ca ...
- Visual Studio 2017 UTF-8 无 BOM 一站式解决办法
问题背景:最近捡起C++,使用VS 2017平台.因为以前的编程习惯,喜欢使用UTF-8 无 BOM 的编码格式,好让自己的代码全球通用.但是VS 2017 对这个问题不是很友善.但最终找到了解决办法 ...
- PHP计算今天、昨天、本周、本月、上月开始时间和结束时间
PHP计算今天.昨天.本周.本月.上月开始时间和结束时间 $today = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y')) ...
- 编译与安装 OpenSSL
编译与安装 OpenSSL prefix 是安装目录,openssldir 是配置文件目录,另外建议安装两次,shared 作用是生成动态连接库.linux版的OpenSSL下载地址为:https:/ ...
- python爬虫基础11-selenium大全5/8-动作链
Selenium笔记(5)动作链 本文集链接:https://www.jianshu.com/nb/25338984 简介 一般来说我们与页面的交互可以使用Webelement的方法来进行点击等操作. ...
- 面试题--如何防止sql注入,使用PreparedStatement的预编译,传入的内容就不会和原来的语句发生任何匹配的关系,达到防止注入的方法
PreparedStatement的用法 jdbc(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.statement要求开发 ...
- python之函数基础总结
定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi(name): print("Hello, %s, I', nobo ...
- Linux学习-什么是例行性工作排程
那么 Linux 的例行性工作是如何进行排程的呢?所谓的排程就是将这些工作安排执行的流程之意! 咱们的 Linux 排程就是透过 crontab 与 at 这两个东西! Linux 工作排程的种类: ...
- 2015多校训练第二场 hdu5305
把这题想复杂了,一直在考虑怎么快速的判断将选的边和已选的边无冲突,后来经人提醒发现这根本没必要,反正数据也不大开两个数组爆搜就OK了,搜索之前要先排除两种没必要搜的情况,这很容易想到,爆搜的时候注意几 ...