Python OS File and Directory Methods: Interacting with the File System

thumb_up 1  ·  sell Python OS file methods, Python OS directory methods, File system interaction in Python

The os module provides a big range of useful methods to manipulate files. Most of the useful methods are listed here −

Sr.No. Methods with Description

1

os.close(fd)

Close file descriptor fd.

2

os.closerange(fd_low, fd_high)

Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors.

3

os.dup(fd)

Return a duplicate of file descriptor fd.

4

os.fdatasync(fd)

Force write of file with filedescriptor fd to disk.

5

os.fdopen(fd[, mode[, bufsize]])

Return an open file object connected to the file descriptor fd.

6

os.fsync(fd)

Force write of file with filedescriptor fd to disk.

7

os.ftruncate(fd, length)

Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size.

8

os.lseek(fd, pos, how)

Set the current position of file descriptor fd to position pos, modified by how.

9

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

Open the file file and set various flags according to flags and possibly its mode according to mode.

10

os.read(fd, n)

Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned.

11

os.tmpfile()

Return a new file object opened in update mode (w+b).

12

os.write(fd, str)

Write the string str to file descriptor fd. Return the number of bytes actually written.

 
 
 
 
 
 
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back