

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read a number of characters from a text file using Python?
To read a file’s contents, you can call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").
So if you want to read 10 ASCII characters, you can simply pass 10 as the argument.
For example
>>> f = open('my_file', 'r') >>> print(f.read(10)) Hello worl >>> f.close()
- Related Questions & Answers
- How to read an entire line from a text file using Python?
- Read Data from a Text File using C++
- How to read a text file in Python?
- How to read a file from command line using Python?
- How to read a text file from resources in Kotlin?
- Counting number of characters in text file using java
- How to read a text file in Selenium with python?
- How to read a text file with C++?
- How to count the number of characters (including spaces) in a text file using Java?
- How to read data from a file using FileInputStream?
- Read integers from a text file with C++ ifstream
- How to read certain number of elements from a file in Java?
- Load a text file and find number of characters in the file - JavaScript
- How to read complete text file line by line using Python?
- How to read text file into a list or array with Python?
Advertisements