Home > Backend Development > Python Tutorial > python os.chdir() definition and function analysis (example analysis)

python os.chdir() definition and function analysis (example analysis)

乌拉乌拉~
Release: 2018-08-17 14:45:31
Original
7447 people have browsed it

In this article today, we will learn about python os.chdir. In the next article, we will introduce os.chdir() in python and introduce it. Definition and its functions and uses.

Overview

The os.chdir() method is used to change the current working directory to the specified path.

Grammar

chdir() method syntax format is as follows:

os.chdir(path)
Copy after login

Parameters

path – the new path to switch to.

Return value

Returns True if access is allowed, otherwise returns False.

Example

The following example demonstrates the use of the chdir() method:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
path = "/tmp"
# 查看当前工作目录
retval = os.getcwd()
print "当前工作目录为 %s" % retval
# 修改当前工作目录
os.chdir( path )
# 查看修改后的工作目录
retval = os.getcwd()
print "目录修改成功 %s" % retval
Copy after login

The output result of executing the above program is:

当前工作目录为 /www
目录修改成功 /tmp
Copy after login

The above is all the content of this article. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of python os.chdir() definition and function analysis (example analysis). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template