python基础读取指令

1、获得当前路径

Python中可以使用os.getcwd()函数获得当前的路径。

其原型如下所示:

os.getcwd()

该函数不需要传递参数,它返回当前的目录。需要说明的是,当前目录并不是指脚本所在的目录,而是所运行脚本的目录。

>>>import  os

>>>print  os.getcwd()

D:\Program Files\Python27 

这里的目录即是python的安装目录。若把上面的两行语句保存为getcwd.py,保存于E:\python\盘,运行后显示是E:\python

2、获得目录中的内容

在Python中可以使用os.listdir()函数获得指定目录中的内容。

其原型如下所示:

os.listdir(path)

其参数path 为要获得内容目录的路径。

>>> import os
>>> print os.listdir(os.getcwd())
3、os.walk()与os.path.walk()

(1)

函数声明:os.walk(top,topdown=True,οnerrοr=None)

(1)参数top表示需要遍历的顶级目录的路径。

(2)参数topdown的默认值是“True”表示首先返回顶级目录下的文件,然后再遍历子目录中的文件。当topdown的值为"False"时,表示先遍历子目录中的文件,然后再返回顶级目录下的文件。

(3)参数onerror默认值为"None",表示忽略文件遍历时的错误。如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历。

返回值:函数返回一个元组,含有三个元素。这三个元素分别是:每次遍历的路径名、路径下子目录列表、目录下文件列表。

os.walk使用实例:删除某个文件夹(当然可以通过os.listdir的递归调用删除)

01 #! /usr/bin/env python
02 #coding=utf-8
03 import os
04  
05 def Remove_dir(top_dir):
06     if os.path.exists(top_dir)==False:
07         print "not exists"
08         return
09     if os.path.isdir(top_dir)==False:
10         print "not a dir"
11         return
12     for dir_path,subpaths,files in os.walk(top_dir,False):
13         for file in files:
14             file_path=os.path.join(dir_path,file)
15             print "delete file:%s"  %file_path
16             os.remove(file_path)
17         print "delete dir:%s" %dir_path
18         os.rmdir(dir_path)
19  
20 #调用
21 Remove_dir(r"C:\Users\Administrator\Desktop\zrbuN7zRuc")

(2)

函数声明:os.path.walk(top,func,arg)

(1)参数top表示需要遍历的目录路径

(2)参数func表示回调函数,即对遍历路径进行处理的函数。所谓回调函数,是作为某个函数的参数使用,当某个时间触发时,程序将调用定义好的回调函数处理某个任务。注意:walk的回调函数必须提供三个参数:第1个参数为os.path.walk的参数arg,第2个参数表示目录dirname,第3个参数表示文件列表names。注意:os.path.walk的回调函数中的文件列表不和os.walk()那样将子目录和文件分开,而是混为了一摊,需要在回调函数中判断是文件还是子目录。

(3)参数arg是传递给回调函数的元组,为回调函数提供处理参数,arg可以为空。回调函数的第1个参数就是用来接收这个传入的元组的。

过程:以top 为根的目录树中的每一个目录 (包含 top 自身,如果它是一个目录),以参数 (arg, dirname, names)调用回调函数 funct。参数 dirname 指定访问的目录,参数 names 列出在目录中的文件(从 os.listdir(dirname)中得到)。回调函数可以修改 names 改变 dirname 下面访问的目录的设置,例如,避免访问树的某一部分。(由 names 关连的对象必须在合适的位置被修改,使用 del 或 slice 指派。) 注意:符号连接到目录不被作为一个子目录处理,并且因此 walk()将不访问它们。访问连接的目录你必须以os.path.islink(file) 和 os.path.isdir(file)标识它们,并且必须调用walk() 。 

os.path.walk使用实例:遍历文件夹下所有文件(os.path.walk()不能用于删除文件夹(可能是我没想到),因为os.path.walk()先遍历顶级目录,再遍历子目录中的文件)。

01 #! /usr/bin/env python
02 #coding=utf-8
03 import os
04 #回调函数
05 def find_file(arg,dirname,files):
06     for file in files:
07         file_path=os.path.join(dirname,file)
08         if os.path.isfile(file_path):
09             print "find file:%s" %file_path
10      
11  
12 #调用
13 os.path.walk(r"C:\Users\Administrator\Desktop\4",find_file,())

 

区别:os.path.walk()与os.walk()产生的文件名列表并不相同.os.walk()产生目录树下的目录路径和文件路径,而os.path.walk()只产生文件路径(是子目录与文件的混合列表)。

4、os.rename()

os.rename() 方法用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError。

语法

rename()方法语法格式如下:

os.rename(src, dst)

参数

  • src -- 要修改的目录名

  • dst -- 修改后的目录名

5、strip()

函数原型

声明:s为字符串,rm为要删除的字符序列

s.strip(rm)        删除s字符串中开头、结尾处,位于 rm删除序列的字符

s.lstrip(rm)       删除s字符串中开头处,位于 rm删除序列的字符

s.rstrip(rm)      删除s字符串中结尾处,位于 rm删除序列的字符

注意:

1. 当rm为空时,默认删除空白符(包括'\n', '\r',  '\t',  ' ')

例如:

复制代码代码如下:

>>> a = '     123'
>>> a.strip()
'123'
>>> a='\t\tabc'
'abc'
>>> a = 'sdff\r\n'
>>> a.strip()
'sdff'

2.这里的rm删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。

例如 :

复制代码代码如下:

>>> a = '123abc'
>>> a.strip('21')
'3abc'   结果是一样的
>>> a.strip('12')
'3abc'

Python中的strip用于去除字符串的首尾字符,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符。

这三个函数都可传入一个参数,指定要去除的首尾字符。

需要注意的是,传入的是一个字符数组,编译器去除两端所有相应的字符,直到没有匹配的字符,比如:

theString =  'saaaay yes no yaaaass'
print  theString.strip( 'say' )

theString依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。所以,输出的结果为: 
yes no 
比较简单吧,lstrip和rstrip原理是一样的。

注意:当没有传入参数时,是默认去除首尾空格的。 

theString =  'saaaay yes no yaaaass'
print  theString.strip( 'say' )
print  theString.strip( 'say ' ) #say后面有空格
print  theString.lstrip( 'say' )
print  theString.rstrip( 'say' )

运行结果: 

yes no 
es no 
yes no yaaaass 
saaaay yes no

6、filter()
格式:
filter(func, seq)
该函数的目的是提取出seq中能使func为true的元素序列。func函数是一个布尔函数,filter()函数调用这个函数一次作用于seq中的每一个元素,筛选出符合条件的元素,并以列表的形式返回。
下面举一个列子说明,假如有个列表,列表中有几个数字,现在我想从这些数字中,选出即能被2整除又能被3整除的数。
<code class="hljs python has-numbering">nums = [<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">6</span>,<span class="hljs-number">12</span>,<span class="hljs-number">15</span>,<span class="hljs-number">18</span>]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">nums_res</span> <span class="hljs-params">(x)</span>:</span>
  <span class="hljs-keyword">return</span> x % <span class="hljs-number">2</span> == <span class="hljs-number">0</span> <span class="hljs-keyword">and</span> x % <span class="hljs-number">3</span> == <span class="hljs-number">0</span>
<span class="hljs-keyword">print</span> filter(nums_res, nums)
执行结果:[<span class="hljs-number">6</span>, <span class="hljs-number">12</span>, <span class="hljs-number">18</span>]</code>
7、remove pop del的区别

remove 是删除首个符合条件的元素。并不是删除特定的索引。如下例: 本文来自Novell迷网站 http://novell.me

 
 
  1. >>> a = [0223
  2. >>> a.remove(2
  3. >>> a 
  4. [023

而对于 del 来说,它是根据索引(元素所在位置)来删除的,如下例:

 
 
  1. >>> a = [3221
  2. >>> del a[1
  3. [321

第1个元素为a[0] --是以0开始计数的。则a[1]是指第2个元素,即里面的值2.

最后我们再看看pop

 
 
  1. >>> a = [435
  2. >>> a.pop(1
  3. 3 
  4. >>> a 
  5. [45

pop返回的是你弹出的那个数值。

所以使用时要根据你的具体需求选用合适的方法。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值