如何使用os.chdir减去最后一步,转到路径? [英] How can you use os.chdir to go to a path minus the last step of it?

查看:156
本文介绍了如何使用os.chdir减去最后一步,转到路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,一个方法传递了一个路径作为参数,该路径可能是"C:/a/b/c/d",如果我想使用os.chdir()更改为C:/a/b/c(没有最后一个文件夹)? os.chdir()可以使用".."命令吗?

For example, a method is passed a path as a parameter, this path might be "C:/a/b/c/d", what if I want to use os.chdir() to change to C:/a/b/c (without the last folder)? Can os.chdir() take the ".." command?

推荐答案

os.chdir()可以将'..'作为参数,是的.但是,Python提供了一种与平台无关的方式:通过使用os.pardir:

os.chdir() can take '..' as argument, yes. However, Python provides a platform-agnostic way: by using os.pardir:

os.chdir(os.pardir)

这篇关于如何使用os.chdir减去最后一步,转到路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆