惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Scott Helme
Scott Helme
Project Zero
Project Zero
E
Exploit-DB.com RSS Feed
S
Secure Thoughts
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
NISL@THU
NISL@THU
WordPress大学
WordPress大学
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
L
LINUX DO - 热门话题
小众软件
小众软件
P
Privacy & Cybersecurity Law Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
A
About on SuperTechFans
Hacker News: Ask HN
Hacker News: Ask HN
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hacker News: Front Page
F
Full Disclosure
Latest news
Latest news
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
T
Troy Hunt's Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Jina AI
Jina AI
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
G
GRAHAM CLULEY
Forbes - Security
Forbes - Security
V
V2EX - 技术
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
MongoDB | Blog
MongoDB | Blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
P
Privacy International News Feed
C
Check Point Blog
N
News and Events Feed by Topic

博客园 - IamV

ADO.NET Entity Framework: 由 Entity Object 執行 SQL 指令 Expression<Func<int, bool>>与<Func<int, bool> search something from db How to Embed Silverlight Content in HTML Flash与3D编程探秘 - 文章目录 silverlight RenderTransform 后台动画设置 2104 Couldn't download the silverlight application Cannot connect to external websites - WebClient - IamV .NET下多线程中设置按钮的文本 - IamV - 博客园 [Desklighter]在桌面上运行你的Silverlight文件 - IamV [转]Silverlight 2.0 正式版跨域提交数据 - IamV Application.DoEvent() - IamV 转:C#线程调用带参数的方法 - IamV [转]深入了解字符集和编码 Office SharePoint Server Search与Windows SharePoint Services Search - IamV 调用WCF返回Josn的两种方式 - IamV asp.net Excel_MyTest - IamV SharePoint-Server Farm Configuration Not Complete - IamV Using JSON with ASP.NET 3.5 - IamV
[转]python文件操作/打开/删除文件/压缩文件
IamV · 2009-01-21 · via 博客园 - IamV

文件操作....是一个语言和外界联系的主要方法....现在以txt为例简单的讲一下...

首先是建立关联...假设在存在以下文件 c:\a.txt

  1. This is line #1
  2. This is line #2
  3. This is line #3
  4. END
  1. >>> xxx = file('c:\\a.txt', 'r')

关键字的第一部分,是

文件

路径及名称。注意这里面,路径需要用

\\

第二部分,是对

文件

的模式或者叫权限,一般有以下3种 "r" (read), "w" (write)和 "a"(append).

之后,就可以利用

xxx_content = infile.read()

xxx_content = infile.readlines()

来读取

文件

内容了

  1. >>> xxx = file('c:\\a.txt', 'r')
  2. >>> xxx_content = xxx.read()
  3. >>> print xxx_content
  4. This is line #1
  5. This is line #2
  6. This is line #3
  7. END
  8. >>> xxx.close()
  9. >>>
  10.  
  11.  
  12.  
  13. >>> infile = file('c:\\a.txt', 'r')
  14. >>> xxx = file('c:\\a.txt', 'r')
  15. >>> for xxx_line in xxx.readlines():
  16. print 'Line:', xxx_line
  17.  
  18.  
  19. Line: This is line #1
  20.  
  21. Line: This is line #2
  22.  
  23. Line: This is line #3
  24.  
  25. Line: END
  26. >>> xxx.close()
  27. >>>

然后是

文件

的写入

  1. >>> xxx=file('c:\\test.txt','w')
  2. >>> xxx.write('billrice')
  3. >>> xxx.write('testtest')
  4. >>> xxx.write('enter\n')
  5. >>> xxx.writelines(['billrice','ricerice'])
  6. >>> xxx.close()
  7. >>>
  8. >>> xxx=file('c:\\test.txt','r')
  9. >>> content=xxx.read()
  10. >>> print content
  11. billricetesttestenter
  12. billricericerice
  13. >>>


需要注意的是...在xxx.close()之前,c盘下面只有一个空空的test.txt,xxx.close()的作用相当于最后的存盘。

删除文件

name='c:\1.txt'   
os.remove(name)

压缩文件

import os
import zipfile
import time
# 压缩目录
source_dir  = r'F:\web'
# 按时间生成文件名称
target_file = time.strftime('%Y%m%d%H%M%S') + '.zip'

myZipFile = zipfile.ZipFile(target_file, 'w' )# 压缩所有文件,包含子目录
for root,dirs,files in os.walk(source_dir):
    for vfileName in files:
        fileName = os.path.join(root,vfileName)
        myZipFile.write( fileName, fileName, zipfile.ZIP_DEFLATED )
 # 压缩完成
myZipFile.close()