Python file() Function | Tutorial
Tutorial -- Learning not just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Python Basic Tutorial
Python Basic Tutorial Python Introduction Python Environment Setup Python Chinese Encoding Python VS Code Python Basic Syntax Python Variable Types Python Operators Python Conditional Statements Python Loop Statements Python While Loop Statement Python for Loop Statement Python Nested Loops Python break Statement Python continue Statement Python pass Statement Python Number Python Strings Python Lists Python Tuples Python Dictionary Python Date and Time Python Functions Python Modules Python File I/O Python File Methods Python Exceptions Python OS File/Directory Methods Python Built-in Functions
Python Advanced Tutorial
Python Object-Oriented Python Regular Expressions Python CGI Programming Python MySQL Python Network Programming Python SMTP Python Multithreading Python XML Parsing Python GUI Programming (Tkinter) Python2.x vs 3.x Differences Python IDE Python JSON Python AI Drawing Python 100 Examples Python Quiz
Python OS File/Directory Methods
Deep Dive
- Scripting Languages
- Computer Science
- Scripting
- Programming
- Programming Languages
- Web Services
- Development Tools
- Software
- Web Design & Development
- Web Service
Python2.x Python file() Function
Description
The file() function is used to create a file object. It has an alias called open(). To be more precise, they are built-in functions. The parameters are passed as strings.
For more file operations, refer to: Python File I/O.
Syntax
Here is the syntax for the file() method:
file(name[, mode[, buffering]])
Parameters
- name -- The file name
- mode -- The mode to open the file
- buffering -- 0 means no buffering, 1 means line buffering, and values greater than 1 specify the buffer size.
Return Value
The file object.
Example
Test file test.txt with the following content:
TUTORIAL1
TUTORIAL2
>>> f = file('test.txt')
>>> f.read()
'TUTORIAL1nTUTORIAL2n'
YouTip