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

来源:互联网 发布:win7 补丁 软件 编辑:程序博客网 时间:2024/04/29 15:14

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/pythonimport os, sys# First go to the "/var/www/html" directoryos.chdir("/var/www/html" )# Print current working directoryprint "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 diros.fchdir(fd)# Print current working directoryprint "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/htmlCurrent 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/pythonimport ospath = "/usr/tmp"# Check current working directory.retval = os.getcwd()print "Current working directory %s" % retval# Now change the directoryos.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 /usrDirectory 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/pythonimport os, sys# Open a filefd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )# Write one stringos.write(fd, "This is test")# Close opened fileos.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/pythonimport os, sys# Open a filefd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )# Write one stringos.write(fd, "This is test")# Close opened fileos.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!!


0 0
原创粉丝点击