for root,dirs,name in oswalk(""):
for files in names:
if files in ("1mp4","thumbpng"):
newname=osbasename(root)
extname=ospathsplitext(files)[-1]
osrename(files,newname+extname)
先要装载 os模块:
import os
print osgetcwd()
或者
print ospathabspath(oscurdir)
print ospathabspath('')
就可以了。
( 代表当前的路径, 代表当前路径的上一级路径。这在UNIX和Windows系统意义是类似的。
例如:
以Winodws系统为例:
当前路径是F:\Temp, 则 的绝对路径就是F:\Temp
的绝对路径就是F:\
)
要获得上级目录的路径也很简单,print ospathabspath('')就可以了。
要改变当前路径,oschdir(path) 就可以了 path里填要改变到的目录,例如oschdir('D:\Program Files')
这样大部分的文件 *** 作现在是相对于D:\Program Files 来了,例如fobj = open('Hellotxt'),实际会打开D:\Program Files\Hellotxt文件。
不要通过sysargv[0]获得当前路径,那是不正确的。sysargv[0] 是当前执行的Python脚本的文件名,不一定是当前的路径。
希望对你有所帮助。
除了osgetcwd()这个方法,还可以通过ospathabspath('')的方法获取当前路径,你试试。
但是我无法重现你的问题,所以不知道ospathabspath('')是否管用。按照你的表述,我写了下面这个测试程序
==================================
import os
print osgetcwd()
import win32comclient
xlApp = win32comclientDispatch('ExcelApplication') #打开EXCEL
xlBook = xlAppWorkbooksOpen('c:\\1xls')
xlSht = xlBookWorksheets('sheet1')
print str(xlShtCells(1,1)Value)
print osgetcwd()
xlBookClose(SaveChanges=1)
del xlApp
=================================
但是两次输出的地址都是一样的。
是不是你的程序还有那个细节没讲清楚?你可以追问。
希望能帮到你!
import os
path = "d:/"
for root,dirs,files in oswalk(path):
dirs得到的是一个列表,元素就是文件夹名
读文本文件
input = open('data', 'r')
#第二个参数默认为r
input = open('data')
读二进制文件
input = open('data', 'rb')
读取所有内容
file_object = open('thefiletxt')
try:
all_the_text = file_objectread( )
finally:
file_objectclose( )
读固定字节
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_objectread(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_objectclose( )
读每行
list_of_all_the_lines = file_objectreadlines( )
如果文件是文本文件,还可以直接遍历文件对象获取每行:
for line in file_object:
process line
以上就是关于python获取当前目录下很多文件夹的名称,然后重命名文件夹下面的文件。全部的内容,包括:python获取当前目录下很多文件夹的名称,然后重命名文件夹下面的文件。、python 怎么查看当前路径、python获取当前路径等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)