- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Python使用os模块和fileinput模块来操作文件目录由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
os模块 在python编程中,我们会经常使用到文件和目录,这些操作python提供一个os模块,里面有很多操作文件和目录的函数. 。
下面介绍os的常用功能 。
1.获取当前路径 。
1
2
|
>>> os.getcwd()
'/root/python'
|
2.获取目录中的内容 。
1
2
|
>>> os.listdir(os.getcwd())
[
'deco.py'
,
'6.py'
,
'inputfile.py'
,
'uniFile.py'
,
'oddnogen.py'
,
'ospathex.py'
,
'operate.py'
,
'bianliang.py'
,
'typecheck1.py'
]
|
3.创建目录 。
1
|
>>> os.mkdir(
'/root/pythontest'
)
|
一次性创建嵌套目录可使用os.makedirs,makedirs可以生成多层递归目录,removedirs可以删除多层递归的空目录,若目录中有文件则无法删除;os.system():运行shell命令; 。
1
2
3
4
5
6
7
8
9
10
11
|
>>> os.environ[
'HOME'
]
'/root'
>>> os.makedirs(os.path.join(os.environ[
'HOME'
],
'test'
,
'py'
))
>>> os.system(
'ls -lR /root/test'
)
/
root
/
test:
total
4
drwxr
-
xr
-
x
2
root root
4096
2013
-
03
-
29
17
:
27
py
/
root
/
test
/
py:
total
0
0
|
4.改变工作目录 。
1
2
3
4
5
|
>>> os.getcwd()
'/root/python'
>>> os.chdir(
'/etc'
)
>>> os.getcwd()
'/etc'
|
5.删除目录 。
1
2
|
>>> os.rmdir(
'/root/pythontest'
)
>>>
|
注:删除目录必须是空目录 。
6.删除文件 。
1
|
>>> os.remove(
'/root/pytest.py'
)
|
7.重命名文件或者目录 。
1
|
>>> os.rename(
'/root/pytest.py'
,
'/root/test.py'
)
|
8.字符串给出当前平台使用的终止符,例如:Windows使用'\r\n',Linux使用'\n' 。
1
2
|
>>> os.linesep
'\n'
|
以下是os.path模块 。
9.判断是否是目录 。
1
2
|
>>> os.path.isdir(
'/root/python'
)
True
|
10.判断是否为文件 。
1
2
|
>>> os.path.isfile(
'/root/python'
)
False
|
11.返回一个路径的目录名和文件名 。
1
2
|
>>> os.path.split(os.getcwd())
(
'/root'
,
'python'
)
|
12.判断是否存在文件或目录name 。
1
2
3
4
|
>>> os.path.exists(
'/root'
)
True
>>> os.path.exists(
'/root/1.py'
)
False
|
13.获得文件大小 。
1
2
|
>>> os.path.getsize(
'/root/python/if.py'
)
282
|
14.返回文件路径,返回文件名 。
1
2
3
4
|
>>> os.path.dirname(
'/root/python/if.py'
)
'/root/python'
>>> os.path.basename(
'/root/python/if.py'
)
'if.py'
|
15.连接目录与文件名或目录 。
1
2
|
>>> os.path.join(
'/dave/test/'
,
'1.py'
)
'/dave/test/1.py'
|
判断一个目录里哪些是文件哪些是目录. 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>>
for
file
in
os.listdir(
'/root/python'
):
...
if
os.path.isdir(
file
):
...
print
file
,
"is a directory"
...
else
:
...
print
file
,
"is a file"
...
deco.py
is
a
file
inputfile.py
is
a
file
operate.py
is
a
file
hello.pyc
is
a
file
userpw.py
is
a
file
4.py
is
a
file
answer
is
a directory
|
fileinput模块 fileinput模块可以遍历文本文件的所有行.它的工作方式和readlines很类似,不同点在于,它不是将全部的行读到列表中而是创建了一个xreadlines对象. 。
下面是fileinput模块中的常用函数 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
>>>
import
fileinput
>>>
for
eachline
in
fileinput.
input
(
"/root/datafile.txt"
):
...
print
eachline
...
Dave
python
>>>
for
eachline
in
fileinput.
input
(
"/root/datafile.txt"
):
...
print
fileinput.filename()
...
/
root
/
datafile.txt
/
root
/
datafile.txt
>>>
for
eachline
in
fileinput.
input
(
"/root/datafile.txt"
):
...
print
fileinput.lineno(),eachline
...
1
Dave
2
python
>>>
for
eachline
in
fileinput.
input
(
"/root/datafile.txt"
):
...
if
fileinput.isfirstline():
...
print
eachline
...
else
:
...
break
...
Dave
|
下面是一个关于日志分析的小实例,将日志中截取匹配的电话号码,如800-333-1212 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
root@
10.1
.
1.45
:~
# cat telphone.log
027
-
987
-
1212
02187899682
021
-
110
-
9632
root@
10.1
.
1.45
:~
# cat fileinputtest.py
#!/usr/bin/python2.5
import
fileinput
import
sys
import
re
pattern
=
"\d{3}-\d{3}-\d{4}"
filePath
=
"/root/telphone.log"
for
eachline
in
fileinput.
input
(filePath):
a
=
re.search(pattern,eachline)
if
a:
# sys.stdout.write("============\n")
# sys.stdout.write(eachline)
print
"============"
print
'line:'
,fileinput.lineno(),
'filename:'
,fileinput.filename(),
'length:'
,
len
(eachline.strip(
'\n'
)),eachline
root@
10.1
.
1.45
:~
# ./fileinputtest.py
=
=
=
=
=
=
=
=
=
=
=
=
line:
1
filename:
/
root
/
telphone.log length:
12
027
-
987
-
1212
=
=
=
=
=
=
=
=
=
=
=
=
line:
3
filename:
/
root
/
telphone.log length:
12
021
-
110
-
9632
|
最后此篇关于Python使用os模块和fileinput模块来操作文件目录的文章就讲到这里了,如果你想了解更多关于Python使用os模块和fileinput模块来操作文件目录的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
在几个 SO 的问题中,有这些行可以访问代码的父目录,例如os.path.join(os.path.dirname(__file__)) returns nothing和 os.path.join(o
我想用 Python 更改文件模式。 os 模块具有三个功能上看似相同的功能: os.chmod os.fchmod os.lchmod 这三个版本有什么区别? 最佳答案 chmod 用于更改路径指定
考虑: pipe_read, pipe_write = os.pipe() 现在,我想知道两件事: (1) 我有两个线程。如果我保证只有一个正在读取 os.read(pipe_read,n) 而另一个
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
让我们以硬盘驱动器/网络接口(interface)为例。它由多个进程共享。现在多个进程可能会向硬盘驱动器发出并发命令来读取数据。当数据可用时,内核如何知道哪个进程的数据已准备好?操作系统和硬盘驱动器之
嗨,我正在尝试编写像这样的原子写入函数...... with tempfile.NamedTemporaryFile(mode= "w", dir= target_directory) as f:
net.Conn接口(interface)提供了 SetTimeout 方法,我应该用 os.Timeout 检查返回的错误.但是我看不到在返回的 os.Error 上调用 os.Timeout 的方
我正在使用 os 模块在我的 Django 项目 settings.py 文件中具有相对路径。变量 SITE_ROOT 设置为 settings.py 文件的当前工作目录,然后用于引用同样位于同一目录
正如我们所知,Windows 接受 "\" 和 "/" 作为分隔符。但是在python中,使用的是"\"。例如,调用 os.path.join("foo","bar"),将返回 'foo\\bar'。
我有以下工作目录:/Users/jordan/Coding/Employer/code_base ,我想要获取绝对路径的文件位于 /Users/jordan/Coding/Employer/code_
在 Python 中,如果路径中包含“~”,我能否确定扩展的用户调用将是绝对路径? 例如,这个表达式是否总是为真? path = '~/.my_app' os.path.expanduser(path
我是 Django 项目的初学者。Django 项目的 settings.py 文件包含这两行: BASE_DIR = os.path.dirname(os.path.dirname(os.path.
我有一个旧 MAC OS 文件存储中的文件集合。我知道集合存在文件名/路径名问题。问题源于我认为在原始操作系统中呈现为破折号的路径中包含一个代码点,但 Windows 与代码点斗争,并且其中一个包含
Ubuntu怎么安装mac os x主题呢?下文小编将为大家分享ubuntu14.04安装mac os x主题教程,安装MAC OS X&
我有一个 Firefox OS 应用程序,我希望在该应用程序之外打开一个链接(该链接指向不同的站点,在应用程序中打开它会使应用程序在没有强制的情况下无法使用)。我怎么做? Related bug re
我想为 Firefox OS 编写我的应用程序.使用什么样的语言(如 Android 的 Java 和 iOS 的 Objective C++)和工具(如 Eclipse、Xcode)? 最佳答案 适
我正在尝试创建一个 Palm OS 应用程序,以每 X 分钟或几小时检查一次网站,并在有数据可用时提供通知。我知道这种事情可以在新的 Palm 上完成——例如,当应用程序不在顶部时,我的 Centro
我需要在 Firefox OS 中显示全屏图像。我有一个具有 qHD 分辨率(960x540 像素)的“峰值”开发预览手机。 如何确保我的应用程序在其他具有不同屏幕分辨率的 firefox-os 设备
我正在尝试在 Firefox OS 中安装一个新的语言环境,但我不确定我是否正确地按照这些步骤操作。 首先,我尝试使用 Mercurial 下载所需的语言环境:它对我不起作用,Mercurial 说访
我有这个shell脚本Test.sh: #! /bin/bash FILE_TO_CHECK="/Users/test/start.txt" EXIT=0 while [ $EXIT -eq 0 ];
我是一名优秀的程序员,十分优秀!