Python自动移动电驴下载完成的文件(未完)

今天整Google App Engine整了一天,用gappproxyFQ,呵呵。时隔一年再动gae觉得爽了很多,官方工具也出了,不必再dos,手动修改yml文件,还可以本地调试。
就是那个gapproxy好像不能处理加密页面,比如facebook twitter google的登录等。一点就打不开网页。这点很郁闷。


切入正题:
由于上班的时候不怎么用网,但这光纤带宽不能浪费啊。于是就下美剧,学英语。电驴p2p协议的大家都知道,你既是下载者,也是资源提供者。
办公室人多,即使下也是限速。就不想和别人多分享了,下完马上移动文件位置。


下午先写了个文件剪切的部分,遇到了一些字符编码的问题,不知道有没有什么办法能避免,代码如下:
py所在文件夹就是电驴的下载文件夹。

 

代码
#encoding=utf-8

import os,shutil
from os import path

files
= os.listdir(os.getcwd())
for onefile in files:
if not path.isfile(onefile):
continue
name, stuffix
= path.splitext(onefile)
if stuffix not in ('.part', '.met', '.py'):
try:
#文件编码为utf-8,所以得到的字符串也是utf-8的,但本地系统编码是gbk的
newName = 'D:\\veryCDOK\\老友记\\4第四季\\'
#先解码成unicode
newName = newName.decode('utf-8')
#然后编码成gbk的
newName = newName.encode('gbk')
#print type(newName)
#print type(onefile)
##print newName
##print onefile
shutil.move(path.abspath(onefile),newName)
except Exception, e:
print e


预留三个问题:
1.我发现python的move不是windows的剪切,而是先copy然后再delete的,这样挺慢。看看明天能否调用windows的剪切功能。
2.准备做根据电影名字,自动创建文件夹,或者归类文件。由于电影的名字里的符号杂乱不已。所以就只能在建立下载任务的时候,规定一个命名格式,初步设想是:[[电影名字]]+原有的其他信息+扩展名 这样我就能提取电影名字,进行自动归类。
3.关于本程序的执行,打算一直开着5min执行一次,或者建立个计划任务,5min执行一次。具体编码明日修改。

 


另外今天又复习了下os os.path和shutil的用法,以下是copy的:

os和os.path模块
os.sep 可以取代操作系统特定的路径分割符。
os.name字符串指示你正在使用的平台。比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'。
os.getcwd()函数得到当前工作目录,即当前Python脚本工作的目录路径。
os.getenv()和os.putenv()函数分别用来读取和设置环境变量。
os.listdir()返回指定目录下的所有文件和目录名。
os.remove()函数用来删除一个文件。
os.system()函数用来运行shell命令。
os.linesep字符串给出当前平台使用的行终止符。例如,Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
os.path.split()函数返回一个路径的目录名和文件名。
os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。
os.path.existe()函数用来检验给出的路径是否真地存在

os.listdir(dirname):列出dirname下的目录和文件
os.getcwd():获得当前工作目录
os.curdir:返回但前目录('.')
os.chdir(dirname):改变工作目录到dirname
os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false
os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
os.path.exists(name):判断是否存在文件或目录name
os.path.getsize(name):获得文件大小,如果name是目录返回0L
os.path.abspath(name):获得绝对路径
os.path.normpath(path):规范path字符串形式
os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
os.path.splitext():分离文件名与扩展名
os.path.join(path,name):连接目录与文件名或目录
os.path.basename(path):返回文件名
os.path.dirname(path):返回文件路径


shutil模块
copy(src, dst)
Copy data and mode bits ("cp src dst").
The destination may be a directory.


copy2(src, dst)
Copy data and all stat info ("cp -p src dst").
The destination may be a directory.


copyfile(src, dst)
Copy data from src to dst
copyfileobj(fsrc, fdst, length=16384)
copy data from file-like object fsrc to file-like object fdst


copymode(src, dst)
Copy mode bits from src to dst


copystat(src, dst)
Copy all stat info (mode bits, atime and mtime) from src to dst


copytree(src, dst, symlinks=False)
Recursively copy a directory tree using copy2().
The destination directory must not already exist.
If exception(s) occur, an Error is raised with a list of reasons.
If the optional symlinks flag is true, symbolic links in the
source tree result in symbolic links in the destination tree; if
it is false, the contents of the files pointed to by symbolic
links are copied.
XXX Consider this example code rather than the ultimate tool.


move(src, dst)
Recursively move a file or directory to another location.
If the destination is on our current filesystem, then simply use
rename. Otherwise, copy src to the dst and then remove src.
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
rmtree(path, ignore_errors=False, onerror=None)
Recursively delete a directory tree.


If ignore_errors is set, errors are ignored; otherwise, if onerror
is set, it is called to handle the error with arguments (func,
path, exc_info) where func is os.listdir, os.remove, or os.rmdir;
path is the argument to that function that caused it to fail; and
exc_info is a tuple returned by sys.exc_info(). If ignore_errors
is false and onerror is None, an exception is raised.

posted @ 2010-10-18 19:00  北冥  阅读(594)  评论(0编辑  收藏  举报