pythonospathjoin
Ⅰ python os.path.join 结果双\\的问题
可以用函数os.path.normpath()保持一致
Ⅱ 如何用Python os.path.walk方法遍历搜索文件内容的操作详解
本文是关于如何用Python os.path.walk方法遍历搜索文件目录内容的操作详解的文章,python 代码中用os.path.walk函数这个python模块的方法来遍历文件,python列出文件夹下的所有文件并找到自己想要的内容。
文中使用到了Python os模块和Python sys模块,这两个模块具体的使用方法请参考玩蛇网相关文章阅读。
Python os.path.walk方法遍历文件搜索内容方法代码如下:
?
041
import os, sys#代码中需要用到的方法模块导入 listonly = False skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files def visitfile(fname, searchKey): global fcount, vcount try: if not listonly: if os.path.splitext(fname)[1] in skipexts: pass elif open(fname).read().find(searchKey) != -1: print'%s has %s' % (fname, searchKey) fcount += 1 except: pass vcount += 1 #www.iplaypy.com def visitor(args, directoryName,filesInDirectory): for fname in filesInDirectory: fpath = os.path.join(directoryName, fname) if not os.path.isdir(fpath): visitfile(fpath,args) def searcher(startdir, searchkey): global fcount, vcount fcount = vcount = 0 os.path.walk(startdir, visitor, searchkey) if __name__ == '__main__': root=raw_input("type root directory:") key=raw_input("type key:") searcher(root,key) print 'Found in %d files, visited %d' % (fcount, vcount)
Ⅲ python os和os.path模块的区别
os 包括os.path
import os 之后要 os.path 来调用
from os import path 后 直接用path来调用就可以了
Ⅳ python中用os.path.join()得到的路径可以用来读取文件里面的文本内容吗
print(text.readlines())
Ⅳ python初学者,遇到个python问题,关于os里面的path.join问题。
可能是你的logDate本身就带有.log后缀!
Ⅵ python os模块怎么使用
常用方法:
1. os.name——判断现在正在实用的平台,Windows 返回 ‘nt'; Linux 返回’posix'。
2. os.getcwd()——得到当前工作的目录。
3. os.listdir()——指定所有目录下所有的文件和目录名。
例:
Ⅶ 如何解决Python中os.path.join的路径拼接问题
是在拼接路径的时候用的。举个例子,
os.path.join(“home”, "me", "mywork")
在Linux系统上会返回
“home/me/mywork"
在Windows系统上会返回
"home\me\mywork"
好处是可以根据系统自动选择正确的路径分隔符"/"或"\"
Ⅷ 如何学习python的os模块
一、os模块概述
Python os模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。(一语中的)
二、常用方法
1、os.name
输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'。
2、os.getcwd()
函数得到当前工作目录,即当前Python脚本工作的目录路径。
3、os.listdir()
返回指定目录下的所有文件和目录名。
>>> os.listdir(os.getcwd())
['Django', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'MySQL-python-wininst.log', 'NEWS.txt', 'PIL-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'RemoveMySQL-python.exe', 'RemovePIL.exe', 'Removesetuptools.exe', 'Scripts', 'setuptools-wininst.log', 'tcl', 'Tools', 'w9xpopen.exe']
>>>
4、os.remove()
删除一个文件。
5、os.system()
运行shell命令。
>>> os.system('dir')
0
>>> os.system('cmd') #启动dos
6、os.sep 可以取代操作系统特定的路径分割符。
7、os.linesep字符串给出当前平台使用的行终止符
>>> os.linesep
'\r\n' #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
>>> os.sep
'\\' #Windows
>>>
8、os.path.split()
函数返回一个路径的目录名和文件名
>>> os.path.split('C:\\Python25\\abc.txt')
('C:\\Python25', 'abc.txt')
9、os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。
>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile('a.txt')
False
10、os.path.exists()函数用来检验给出的路径是否真地存在
>>> os.path.exists('C:\\Python25\\abc.txt')
False
>>> os.path.exists('C:\\Python25')
True
>>>
11、os.path.abspath(name):获得绝对路径
12、os.path.normpath(path):规范path字符串形式
13、os.path.getsize(name):获得文件大小,如果name是目录返回0L
14、os.path.splitext():分离文件名与扩展名
>>> os.path.splitext('a.txt')
('a', '.txt')
15、os.path.join(path,name):连接目录与文件名或目录
>>> os.path.join('c:\\Python','a.txt')
'c:\\Python\\a.txt'
>>> os.path.join('c:\\Python','f1')
'c:\\Python\\f1'
>>>
16、os.path.basename(path):返回文件名
>>> os.path.basename('a.txt')
'a.txt'
>>> os.path.basename('c:\\Python\\a.txt')
'a.txt'
>>>
17、os.path.dirname(path):返回文件路径
>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'
Ⅸ python 中 [path = os.path.join(*self.directory)],此处*如何理解
*这里是参数展开
例如:
directory = ["C", "pic", "18x.jpg"]
os.path.join(*directory) #相当于os.path.join("C", "pic", "18x.jpg")
>>> "C\\pic\\18x.jpg"
Ⅹ 一般python中的os.path.join()什么时候会用到呢
是在拼接路径的时候用的。举个例子,
os.path.join(“home”, "me", "mywork")
在Linux系统上会返回
“home/me/mywork"
在Windows系统上会返回
"home\me\mywork"
好处是可以根据系统自动选择正确的路径分隔符"/"或"\"