fileIO和OS操作文件和目录
2024-08-26 20:05:47
1.FileIO操作文件
# 文件IO,读取文件和创建文件
# 1.读取键盘输入
x=input("please input number")
print("您输入的是"+x) # 2.打开一个文件(open函数相当于创建一个file对象)
fo = open("test.sh", "r+")
print("文件名: ", fo.name)
print("是否已关闭 : ", fo.closed)
print("访问模式 : ", fo.mode)
print("文件位置 : ", fo.tell()) # 3.去读打开的上面的文件进行操作(传负数代表读取所有)
content = fo.read(-1)
print(content) # 4.向文件中写内容
fo.write("llllllllllllll")
fo.flush()
content1 = fo.read(-1)
print(content1)
2.OS模块处理文件和目录
# OS模块处理文件和目录
import os
# 打印当前目录
print(os.getcwd())
# 打印环境变量
print(os.getenv("path"))
# 改变工作目录
path="C:/Users/liqiang/Desktop"
os.chdir(path)
print(os.getcwd()) # 删除目录
os.removedirs("pythonDir")
# 创建目录
os.mkdir("pythonDir")
# 进入当前目录的pythonDir子目录
os.chdir("pythonDir")
print(os.getcwd())
# 创建两个文件
open("test1.txt",'w')
open("test2.txt",'w')
# 列出文件夹下的文件内容
print(os.listdir(os.getcwd()))
最新文章
- maven_spring mvc_mina_dome(实体,文件,批传)(spring mina 初学dome)
- 转:认识MyBean
- 【8-30】oracle数据库学习
- 机器学习实战 - 读书笔记(12) - 使用FP-growth算法来高效发现频繁项集
- HashTable Dictionary HashMap
- 我用的/etc/vimrc
- 机器学习公开课笔记(3):Logistic回归
- Google地图接口API之Google地图 API 参考手册(七)
- sql左连接,右连接,内连接
- 《黄聪:手机移动站SEO优化教程》3、如何禁止百度对PC网站进行自动转码
- BZOJ2299: [HAOI2011]向量
- C#技术------垃圾回收机制(GC)
- C# 判断字符串是否可以转化为数字
- PHP学习笔记,自己动手写个MVC的框架 -- base所有代码
- 在Servlet中使用JSON
- jdk 环境变量配置方法总结
- ORACLE SEQUENCE的简单介绍
- G面经Prepare: Print Zigzag Matrix
- SELECT INTO和INSERT INTO SELECT的区别
- MySQL事务(学习笔记)
热门文章
- centos中apache自用常用额外配置记录(xwamp)
- http://www.pythonchallenge.com/ 网站题解
- 洛谷 P2900 [USACO08MAR]土地征用Land Acquisition 解题报告
- python创建多维列表
- iOS之富文本(一)
- Codeforces 931.F Teodor is not a liar!
- mysql日志配置
- windows 安装 apache 服务以及添加 php 解析
- web系统中上下移动功能的实现
- stout代码分析之零