python 查找文件夹下所有文件 实现代码

2024-06-23

python 查找文件夹下所有文件 实现代码(共4篇)

1.python 查找文件夹下所有文件 实现代码 篇一

2014-04-04python模拟登陆阿里妈妈生成商品推广链接

2008-09-09Python strip lstrip rstrip使用方法

2013-03-03python 字符串split的用法分享

2013-11-11python二叉树的实现实例

2013-11-11教你安装python Django(图文)

2014-04-04python判断、获取一张图片主色调的2个实例

2014-05-05Python getopt模块处理命令行选项实例

2014-01-01使用scrapy实现爬网站例子和实现网络爬虫(蜘蛛)的步骤

2009-01-01Python Mysql数据库操作 Perl操作Mysql数据库

2013-11-11linux环境下安装pyramid和新建项目的步骤

★ 用英语的脑筋急转弯

★ 猜 谜 语

★ 春节灯谜-成语谜

★ 什么马用两只脚走路脑筋急转弯

★ 爱我所有作文

★ 解开心结的高一作文

★ 解开冰冻的心扉作文

★ 解开心结初二作文600字

★ 脑筋急转弯大全及

★ 脑筋急转弯全集

2.python 查找文件夹下所有文件 实现代码 篇二

这篇文章主要介绍了Python实现给文件添加内容及得到文件信息的方法,可实现从文件开头添加内容的功能,需要的朋友可以参考下

本文实例讲述了Python实现给文件添加内容及得到文件信息的方法,分享给大家供大家参考。具体分析如下:

经常会遇到给文件添加内容的时候,如果只是添加在文件的末尾,就比较简单了:

file = open(filename,‘a‘)file.write(‘hello‘)file.close

使用‘a‘模式打开文件后,指针默认指向文件末尾,即使你:

file.seek(0)file.write(‘world‘)

字符串‘world‘还是会加在文件的末尾,而不会是你想要的开始位置。

而我遇到的需求就是要在文件头添加东西啊,怎么办呢?不至于把里面东西全读出来,再写进去吧?

还好看到了‘r+‘这个模式(以前从来没有用过)

file = open(filename,‘r+‘)file.tell() #0Lfile.write(‘begin‘)file.close()

打开文件看看,是不是可以了呢;)

得到文件的修改时间:

>>> t = os.path.getmtime(path)>>> t1190626843>>> type(t)>>> os.stat(path)[8]1190626843

得到文件的大小:

>>> os.stat(path)[6]2808L>>> os.path.getsize(path)2808L

3.python 查找文件夹下所有文件 实现代码 篇三

这里将某个目录下的所有文件从一种编码转换为另一种编码,然后保存

import osimport shutildef match(config,fullpath,type): flag=False if type == ‘exclude‘: for item in config[‘src‘][‘exclude‘]:if fullpath.startswith(config[‘src‘][‘path‘]+os.path.sep+item): flag=True break if type==‘filter‘: for item in config[‘src‘][‘filter‘]:if fullpath.endswith(item): flag=True break return flagdef conver_file(param): for root, dirs, files in os.walk(param[‘src‘][‘path‘]): for filename in files:readfile=root+os.path.sep+“%s” %filenameprint(readfile)if ‘filter‘ in param[‘src‘]: if not (match(param,readfile,‘filter‘)): continues=‘‘utfile=readfile.replace(param[‘src‘][‘path‘],param[‘dest‘][‘path‘])try : s=open(readfile,encoding=param[‘src‘][‘encoding‘]).read()except: print(“file %s read erro” % readfile) shutil.copy(readfile,outfile)if s: #False and print(“save”) with open(outfile, mode=‘w‘, encoding=param[‘dest‘][‘encoding‘]) as a_file: a_file.write(s) for dirname in dirs:file=root+os.path.sep+“%s” %dirnameif ‘exclude‘ in param[‘src‘]: if(match(param,file,‘exclude‘)): continueutdir=file.replace(param[‘src‘][‘path‘],param[‘dest‘][‘path‘])#print(outdir)if not os.path.isdir(outdir): os.mkdir(outdir)if __name__ == “__main__”: param={‘src‘:{‘path‘:r‘D:worktesttrunk‘,‘encoding‘:‘gbk‘,‘exclude‘:[‘dataa‘],‘filter‘:[‘.php‘,‘.html‘,‘.htm‘]}, ‘dest‘:{‘path‘:“f:testnew”,‘encoding‘:‘utf-8‘}} conver_file(param)

4.GO语言实现文件上传代码 篇四

功能很简单,代码也很简洁,这里就不多废话了。

代码如下:

package main

import (

“fmt”

“io”

“net/http”

“os”

)

const (

upload_path string = “./upload/”

)

func helloHandle(w http.ResponseWriter, r *http.Request) {

io.WriteString(w, “hello world!”)

}

//上传

func uploadHandle(w http.ResponseWriter, r *http.Request) {

//从请求当中判断方法

if r.Method == “GET” {

io.WriteString(w, “我的第一个页面上传图片

”)

} else {

//获取文件内容 要这样获取

file, head, err := r.FormFile(“file”)

if err != nil {

fmt.Println(err)

return

}

defer file.Close()

//创建文件

fW, err := os.Create(upload_path + head.Filename)

if err != nil {

fmt.Println(“文件创建失败”)

return

}

defer fW.Close()

_, err = io.Copy(fW, file)

if err != nil {

fmt.Println(“文件保存失败”)

return

}

//io.WriteString(w, head.Filename+“ 保存成功”)

http.Redirect(w, r, “/hello”, http.StatusFound)

//io.WriteString(w, head.Filename)

}

}

func main() {

//启动一个http 服务器

http.HandleFunc(“/hello”, helloHandle)

//上传

http.HandleFunc(“/image”, uploadHandle)

err := http.ListenAndServe(“:8080”, nil)

if err != nil {

fmt.Println(“服务器启动失败”)

return

}

fmt.Println(“服务器启动成功”)

}

上一篇:泰戈尔诗三首下一篇:关于品味时尚作文高一

热搜文章

    相关推荐