---
title: Python File Handling
description: "> ## Python File Open – Python файл нээх > > > > File handling is an important part of any web application. > > > > Файл боловсруулах нь аливаа вэб аппликейшны чухал хэсэг юм. > > > > Python…"
url: "https://apprentice.mn/docs/python-file-handling/"
updated: "2024-08-05"
category: Python
---

# Python File Handling

> ## Python File Open – Python файл нээх
>
>
>
> File handling is an important part of any web application.
>
>
>
> Файл боловсруулах нь аливаа вэб аппликейшны чухал хэсэг юм.
>
>
>
> Python has several functions for creating, reading, updating, and deleting files.
>
>
>
> Python нь файлуудыг үүсгэх, унших, шинэчлэх, устгах олон функцтэй.

## File Handling – Файл боловсруулах

The key function for working with files in Python is the `open()` function.

Python-д файлтай ажиллах гол функц бол `open()` функц юм.

The `open()` function takes two parameters; *filename*, and *mode*.

`open()` функц нь хоёр параметрийг авдаг; *файлын нэр* болон *горим*.

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

Файлыг нээх дөрвөн өөр аргууд (горимууд) байдаг:

`"r" - Read - Default value. Opens a file for reading, error if the file does not exist"r" - Унших - Үндсэн утга. Файлыг уншихад нээдэг, файл байхгүй бол алдаа гарна.```

`"a" - Append - Opens a file for appending, creates the file if it does not exist"a" - Нэмэх - Файлыг нэмэхэд нээдэг, файл байхгүй бол үүсгэнэ.`

`"w" - Write - Opens a file for writing, creates the file if it does not exist"w" - Бичих - Файлыг бичихэд нээдэг, файл байхгүй бол үүсгэнэ.`

`"x" - Create - Creates the specified file, returns an error if the file exists"x" - Үүсгэх - Заасан файлыг үүсгэдэг, файл байгаа бол алдаа гаргана.`

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

Мөн файлыг бинар болон текст горимд ажиллуулах эсэхийг тодорхойлох боломжтой

`"t" - Text - Default value. Text mode"t" - Текст - Өгөгдмөл утга. Текст горим.`

`"b" - Binary - Binary mode (e.g. images)"b" - Бинар - Бинар горим (жишээ нь: зургууд).`

## Syntax – Синтакс

To open a file for reading it is enough to specify the name of the file:

Файл уншихын тулд файлын нэрийг зааж өгөхөд хангалттай:

`f = open("demofile.txt")`

The code above is the same as:

Дээрх код нь дараахтай ижил:

`f = open("demofile.txt", "rt")`

Because `"r"` for read, and `"t"` for text are the default values, you do not need to specify them.

Учир нь `"r"` унших, `"t"` текст горим нь өгөгдмөл утгууд тул зааж өгөх шаардлагагүй.

**Note:** Make sure the file exists, or else you will get an error.

**Анхаар:** Файл байгаа эсэхийг шалгаарай, эс тэгвээс алдаа гарна.

            [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-file-handling%2F&linkname=Python%20File%20Handling](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-file-handling%2F&linkname=Python%20File%20Handling)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-file-handling%2F&linkname=Python%20File%20Handling](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-file-handling%2F&linkname=Python%20File%20Handling)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-file-handling%2F&linkname=Python%20File%20Handling](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-file-handling%2F&linkname=Python%20File%20Handling)[https://www.addtoany.com/share](https://www.addtoany.com/share)
