Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.getcwd() method

Python os.getcwd() method

Python File methods Python OS file/directory methods


Overview

The os.getcwd() method returns the current working directory.

Syntax

The **getcwd()** method syntax is as follows:



os.getcwd()

Parameters

Return Value

Returns the working directory of the current process.

Example

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

Example

#!/usr/bin/python

-- coding: UTF-8 --

import

os

,

sys

Switch to the "/var/www/html" directory

os

.

chdir

(

"/var/www/html"

)

Print the current directory

print

"Current working directory : %s"

%

os

.

getcwd

(

)

Open "/tmp"

fd

=

os

.

open

(

"/tmp"

,

os

.

O_RDONLY

)

Use the os.fchdir() method to change the directory

os

.

fchdir

(

fd

)

Print the current directory

print

"Current working directory : %s"

%

os

.

getcwd

(

)

Closing a File

os

.

close

(

fd

)

The output of the above program is:



当前工作目录 : /var/www/html

当前工作目录 : /tmp

Python File methods Python OS file/directory methods