Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Files, Modules and Packag...
  5. Files
  6. File Operations
  7. Opening a file

Opening a file

File Open

  • A file can be opened using built – in open( ) function
  • No module is required
  • The file should be exist in the same directory as python program files else full path name has to be specified as file name
  • The open() function takes two parameters; filename and mode

There are four different methods (modes) for opening a file,

In addition, one can specify if the file should be handled as binary or text mode

Syntax

Fileobject = open(filename,access_mode)
filename is the name of the file to open, access_mode is the method in which we open to open. It is optional. If not specified, the default value is “rt”

The syntax above is same as

Fileobject = open(filename,”rt”)
  • Because “r” for read and “t” for text are default values
  • If the file does not exist, then it produces error

Views: 1

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments