python os.open() os.fchdir()os.chdir() os.close()

1   os.fchdir()

def fchdir(fildes): # real signature unknown; restored from __doc__
    """
    fchdir(fildes)
    
    Change to the directory of the given file descriptor.  fildes must be
    opened on a directory, not a file.
    """
    pass

Description

The method fchdir() change the current working directory to the directory represented by the file descriptor fd. The descriptor must refer to an opened directory, not an open file.

Syntax

Following is the syntax for fchdir() method:

os.fchdir(fd);

Parameters

  • fd -- This is Directory descriptor.

Return Value

This method does not return any value.

Example

The following example shows the usage of fchdir() method.

#!/usr/bin/python

import os, sys

# First go to the "/var/www/html" directory
os.chdir("/var/www/html" )

# Print current working directory
print "Current working dir : %s" % os.getcwd()

# Now open a directory "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )

# Use os.fchdir() method to change the dir
os.fchdir(fd)

# Print current working directory
print "Current working dir : %s" % os.getcwd()

# Close opened directory.
os.close( fd )

When we run above program, it produces following result:

Current working dir : /var/www/html
Current working dir : /tmp

2.  os.chdir()

def chdir(path): # real signature unknown; restored from __doc__
    """
    chdir(path)
    
    Change the current working directory to the specified path.
    """
    pass

Description

The method chdir() changes the current working directory to the given path.It returns None in all the cases.

Syntax

Following is the syntax for chdir() method:

os.chdir(path)

Parameters

  • path -- This is complete path of the directory to be changed to a new location.

Return Value

This method does not return any value.

Example

The following example shows the usage of chdir() method.

#!/usr/bin/python
import os

path = "/usr/tmp"

# Check current working directory.
retval = os.getcwd()
print "Current working directory %s" % retval

# Now change the directory
os.chdir( path )

# Check current working directory.
retval = os.getcwd()

print "Directory changed successfully %s" % retval

When we run above program, it produces following result:

Current working directory /usr
Directory changed successfully /usr/tmp


3.   os.close()

def close(fd): # real signature unknown; restored from __doc__
    """
    close(fd)
    
    Close a file descriptor (for low level IO).
    """
    pass

Description

The method close() closes the associated with file descriptor fd.

Syntax

Following is the syntax for close() method:

os.close(fd);

Parameters

  • fd -- This is the file descriptor of the file.

Return Value

This method does not return any value.

Example

The following example shows the usage of close() method.

#!/usr/bin/python

import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
os.write(fd, "This is test")

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

When we run above program, it produces following result:

Closed the file successfully!!


4. os.open()

Description

The method open() opens the file file and set various flags according to flags and possibly its mode according to mode.The default mode is 0777 (octal), and the current umask value is first masked out.

Syntax

Following is the syntax for open() method:

os.open(file, flags[, mode]);

Parameters

  • file -- File name to be opened.

  • flags -- The following constants are options for the flags. They can be combined using the bitwise OR operator |. Some of them are not available on all platforms.

    • os.O_RDONLY: open for reading only

    • os.O_WRONLY: open for writing only

    • os.O_RDWR : open for reading and writing

    • os.O_NONBLOCK: do not block on open

    • os.O_APPEND: append on each write

    • os.O_CREAT: create file if it does not exist

    • os.O_TRUNC: truncate size to 0

    • os.O_EXCL: error if create and file exists

    • os.O_SHLOCK: atomically obtain a shared lock

    • os.O_EXLOCK: atomically obtain an exclusive lock

    • os.O_DIRECT: eliminate or reduce cache effects

    • os.O_FSYNC : synchronous writes

    • os.O_NOFOLLOW: do not follow symlinks

  • mode -- This work in similar way as it works for chmod() method.

Return Value

This method returns the file descriptor for the newly opened file.

Example

The following example shows the usage of open() method.

#!/usr/bin/python

import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
os.write(fd, "This is test")

# Close opened file
os.close( fd )

print "Closed the file successfully!!"

This would create given file foo.txt and then would write given content in that file and would produce following result:

Closed the file successfully!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值