2006年03月09日 星期四 20:27
我的程序是这样的,我想从本地的一个文件夹里对取文件名,然后打开,然后把文件内容作为mail的内容,把文件不为空第一行作为mail的标题,但是始终是有编码困扰。请大家帮助,谢谢。
我如果把utf-8改成gb2312的话,也有问题:(
source:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
import smtplib
import string
from email.MIMEText import MIMEText
from email.Header import Header
filedir = "/home/book"
filenamelist=[]
def CreateMail(FileString,FileTitle):
""" Creat the mail content."""
Msg = MIMEText(FileString,_subtype='plain',_charset='utf-8')
Msg ['Subject'] = Header(FileTitle,"utf-8")
Msg ['From'] = "******@gmail.com"
Msg ['To'] = "******@gmail.com"
print Msg
"""
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail("******@gmail.com","******@gmail.com",Msg.as_string())
smtp.close()
"""
def ReadWholeFile(Path):
""" Read the special file."""
fd = open(Path,'r')
Msg = fd.readlines()
fd.close()
return Msg
def CheckOutTitle(String):
for i in String:
if i.lstrip().rstrip() != "":
return i
return None
def visit(arg, dirname, names, flist = filenamelist):
flist += [dirname + "/" + file for file in names]
def WalkTheFolder():
os.path.walk(filedir, visit, 0)
def MakeTheMeg():
WalkTheFolder()
for i in filenamelist:
FileName = os.path.split(i)
StrName = FileName[-1].split(".")[-1]
if StrName == ("TXT") or StrName == ("txt") :
String = ReadWholeFile(i)
Title = CheckOutTitle(String)
print Title
CreateMail(String,Title)
if __name__ == '__main__':
MakeTheMeg()
--
/*************************************************************/
* 太初有道,道与神同在,道就是神。_约翰福音(1:1) *
/*************************************************************/
2006年03月09日 星期四 20:38
test --
2006年03月09日 星期四 20:46
??? On 3/9/06, 邱立宏 <qiulihong at gmail.com> wrote: > test > -- > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -- /*************************************************************/ * 太初有道,道与神同在,道就是神。_约翰福音(1:1) * /*************************************************************/
2006年03月10日 星期五 10:41
在 06-3-9,强<freebsd.unix at gmail.com> 写道: 我测试了一下,感觉问题应该在你的文件的编码问题上。 不知道你要打开的文本文件是不是utf-8编码的,而且,如果你要在py里面写中文的话,也要保证保存的编码与你设定的头# -*- coding:utf-8 -*-信息是一样的。 还有,要考虑一下你的终端的默认编码。 看上去你是用linux平台的,一般来说,终端是ansi的。 我在windows下做了一个读写打印文件的测试,发现保存文件的编码不匹配的话是会造成乱码的。 至于转码的操作,很久以前都已经讨论的很透彻了,查一下以前的邮件,或者去啄木鸟找一下吧。
2006年03月26日 星期日 01:23
我对python的网络部分不大了解 借问大家,只使用python,不要混合c或者其他底层的语言可以实现ping吗? 欢迎您的回答
2006年03月26日 星期日 01:51
我曾经看到过一个Python的ICMP的包,不过很老了,是Python 2.1的。 Python没有官方的ICMP的library,只有自己写,要用struct和socket。 On 3/25/06, Jay <wz12 at yeah.net> wrote: > 我对python的网络部分不大了解 > 借问大家,只使用python,不要混合c或者其他底层的语言可以实现ping吗? > 欢迎您的回答 > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > >
2006年03月26日 星期日 13:31
> 我对python的网络部分不大了解 > 借问大家,只使用python,不要混合c或者其他底层的语言可以实现ping吗? > 欢迎您的回答 可以实现,在google上检索一下,去年曾查到过。好像官方python的 邮件列表上有人给过完整的python代码。如果想偷懒,可以用popen2模块直接 调用操作系统提供的ping
2006年03月27日 星期一 11:47
Jay wrote: >我对python的网络部分不大了解 >借问大家,只使用python,不要混合c或者其他底层的语言可以实现ping吗? >欢迎您的回答 > 您google了么? 提示: 可以使用如下关键词 python ping module
2006年03月27日 星期一 17:05
http://pylibpcap.sourceforge.net/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060327/049a220c/attachment.html
Zeuux © 2025
京ICP备05028076号