首页 > Python > python_01提取文件夹里文件名

python_01提取文件夹里文件名

  Python像一个雕刻刀。
  见识一下python的强大,简短的几句,就能完成一个工作,虽然C,java都能实现,但是比较起来,还是python简单。比如提取文件名。

# --- picknames.py ---  
import os  
filenames=os.listdir(os.getcwd())  
for name in filenames:  
    filenames[filenames.index(name)]=name[:-3]  
out=open('names.txt','w')  
for name in filenames:  
    out.write(name+'\n')  
out.close()

解释一下:
1.导入os模块
2.
①getcwd(…)
getcwd() -> path
Return a unicode string representing the current working directory.
②listdir(…)
listdir([path]) -> list_of_strings
Return a list containing the names of the entries in the directory.
path: path of directory to list (default: ‘.’)
The list is in arbitrary order. It does not include the special
entries ‘.’ and ‘..’ even if they are present in the directory.
filenames接收了一个列表
3.4.截取一下字符串,就是把后面的后缀名给截去
③index(…)
index(value, [start, [stop]]) -> integer —
return first index of value.
5.打开一个文件
6.7.往文件中写下弄好的list
8.关闭文件
备注:①②③来自module docs

  1. Sylwia 9月 3rd, 2012 @ 03:47 | #1

    Thanks for seipndng time on the computer (writing) so others don’t have to.

评论提交中, 请稍候...

留言


可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Trackbacks & Pingbacks ( 0 )
  1. 还没有 trackbacks