- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
303 Views
The file truncate() method is quite effective. The first reason it is referred to as a method rather than a function is that it comprises the file name (i.e., the object of the file) and a dot operator in addition to other factors (i.e. object of file). Truncation means cutting off. In this case, we define cutting-off in terms of size. Syntax Following is the syntax used to truncate the file to a given size − file.truncate(size) Sending the parameter to this method is not essential. If the size is given as a parameter, it must be given ... Read More
5K+ Views
The read function reads the whole file at once. You can use the readlines function to read the file line by line.ExampleYou can use the following to read the file line by line:f = open('my_file.txt', 'r+') for line in f.readlines(): print line f.close()You can also use the with...open statement to open the file and read line by line. For example,with open('my_file.txt', 'r+') as f: for line in f.readlines(): print line
13K+ Views
There are various ways to read files in Python. The most common methods for reading files line by line in Python will be covered. Using readlines() Method Using this method, a file will be opened and its contents will be divided into separate lines. A list of every line in the file is also returned by this method. To effectively read a whole file, we can use the readlines() function. Following is an example to remove swap files using file.endswith() method − Example Following is an example to read a text file line by line using readlines() method − # ... Read More
6K+ Views
Python has a built-in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s). Let us understand how to open a file in python. Python is a good general purpose programming language that has a lot of helpful file IO functions and modules in its standard library. You can use the built in open() function to open a file object for either reading or writing. You can use it to open a file in the following way. Syntax The syntax ... Read More
687 Views
Python has built in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s). Let us understand how to open a file in python. Python is a good general purpose programming language that has a lot of helpful file IO functions and modules in its standard library. You can use the built-in open() function to open a file object for either reading or writing. You can use it to open a file in the following way. Syntax The syntax for ... Read More
635 Views
Python has a wide array of modules and tools for solving real-world problems; among them, the os module provides various functions for working with the operating system and performing several useful file operations. Two commonly used functions in the os module, namely, os.open() and os.fdopen() are used for opening files. While both functions serve similar purposes, there are important differences between them. In this article, we will explore the differences between os.open() and os.fdopen() functions; we will understand their respective use cases, and provide code examples to illustrate their utility and usage. Before getting started with exploring the differences ... Read More
4K+ Views
File descriptors in Python are identifiers that represents the open files in the os kernel and are kept in a table of files. Typically, they have non-negative values. Negative results denote an error or a "no value" condition. They support a variety of file-related operations. In general, descriptors are a special approach Python uses to maintain attributes. The main things they assist with are accessing files and other input/output devices like network sockets or pipes. These operations take place on I/O streams identified by following file descriptors − close( fd ) File descriptor closure. This function must be used with ... Read More
2K+ Views
The purpose of internal buffers, which are created by the runtime, library, and programming language that you're using, is to speed up operations by preventing system calls on every write. Instead, when writing to a file object, you write into its buffer, and when the buffer is full, system functions are used to write the data to the actual file. Syntax Following is the syntax of flush() function − File_name.flush() No parameters are accepted by it. This method returns nothing; its return type is . Example -1 The flush() method in the program below simply clears the file's internal ... Read More
2K+ Views
Open(), a built-in function in Python, opens a file and returns a file object. Methods and properties in file objects can be used to gather data about the file you opened. They may also be used to modify the mentioned file. Open a file Two arguments are required for this function. The filename and full path are listed first, followed by access mode. A file object is returned by this function. syntax Following is the syntax used to open a file: open(filename, mode) Here, the filename and it's path are specified by a string argument, and the mode argument ... Read More
4K+ Views
There are times when scenarios arise where you need to delete a directory and all its contents, including subdirectories and files, in the domain of Python programming. Such an operation as above is known as removing a directory recursively. Python makes available a powerful module called shutil that makes it possible for you to perform this task with ease. In this article, we will explore several different ways of how to remove a directory recursively using Python. We will walk you through the process in logical steps, citing code examples and explanations along the way. So, let's get started right ... Read More