Python文件读写+创建临时文件夹的基本方法
2017-02-27 19:41
906 查看
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
def makeTempDir():
"""
设定缓存目录,为当前文件夹的temp文件夹
如果没有对应文件夹则创建文件夹
:return: temp_pwd
"""
temp_pwd = os.path.join(os.getcwd(), 'temp') # 文件缓存目录为当前文件夹下的temp文件夹
if os.path.exists(temp_pwd) == False:
os.makedirs(temp_pwd)
return temp_pwd
def test():
temp_pwd = makeTempDir()
with open(os.path.join(temp_pwd,'test.txt'), 'w') as f: #写文件,没有文件则会创建
for i in range(10):
f.write("python\n")
with open(os.path.join(temp_pwd,'test.txt'), 'r') as f:
lines = f.readlines() #一次性读所有的行
print lines
for line in lines[5:]: #只处理读出来的后5行
line = line.strip('\n')
print line
def main():
test()
if __name__ == '__main__':
main()
# -*- coding: UTF-8 -*-
import os
def makeTempDir():
"""
设定缓存目录,为当前文件夹的temp文件夹
如果没有对应文件夹则创建文件夹
:return: temp_pwd
"""
temp_pwd = os.path.join(os.getcwd(), 'temp') # 文件缓存目录为当前文件夹下的temp文件夹
if os.path.exists(temp_pwd) == False:
os.makedirs(temp_pwd)
return temp_pwd
def test():
temp_pwd = makeTempDir()
with open(os.path.join(temp_pwd,'test.txt'), 'w') as f: #写文件,没有文件则会创建
for i in range(10):
f.write("python\n")
with open(os.path.join(temp_pwd,'test.txt'), 'r') as f:
lines = f.readlines() #一次性读所有的行
print lines
for line in lines[5:]: #只处理读出来的后5行
line = line.strip('\n')
print line
def main():
test()
if __name__ == '__main__':
main()
相关文章推荐
- python 读写、创建 文件的方法(必看)
- python创建临时文件夹的方法
- Python遍历文件夹和读写文件的方法
- python 读写、创建 文件的方法(必看)
- Python遍历文件夹和读写文件的方法
- python 读写、创建 文件的方法(必看)
- Python遍历文件夹和读写文件的实现方法
- Python遍历文件夹和读写文件的方法
- python 读写、创建 文件的方法(必看)
- Python遍历文件夹和读写文件的方法
- python创建临时文件夹的方法
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
- 创建临时文件的简单方法
- python中读写文件及中文编码处理方法
- java 创建文件夹,读写文件
- ubuntu创建、删除文件及文件夹,强制清空回收站方法
- python创建子进程,以及读取配置文件方法
- 读取文件夹下所有文件方法及CFileFind类基本用法
- Ubuntu下创建、重命名、删除文件及文件夹,强制清空回收站方法
- ubuntu创建、删除文件及文件夹,强制清空回收站方法