> For the complete documentation index, see [llms.txt](https://kaze.gitbook.io/qa-theory/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kaze.gitbook.io/qa-theory/kodovye-zadachi/napishite-programmu-kotoraya-prinimaet-imya-faila-i-vyvodit-ego-rasshirenie.-esli-rasshirenie-u-fail.md).

# Напишите программу, которая принимает имя файла и выводит его расширение. Если расширение у файла оп

```python
def get_file_extension(file_name):
    try:
        # Разделяем имя файла и расширение
        file_parts = file_name.split(".")
        
        # Проверяем, есть ли у файла расширение
        if len(file_parts) > 1:
            # Если есть, возвращаем последнюю часть
            return file_parts[-1]
        else:
            # Если расширение отсутствует, выбрасываем исключение
            raise ValueError("Файл не имеет расширения.")
    except Exception as e:
        # Ловим исключение и выводим сообщение об ошибке
        print(f"Ошибка: {e}")

# Пример использования
file_name = input("Введите имя файла: ")
extension = get_file_extension(file_name)

if extension:
    print(f"Расширение файла: {extension}")
```

```python
def get_file_extension(file_name):
    file_parts = file_name.split(".")
    return file_parts[-1] if len(file_parts) > 1 else ""

# Пример использования
file_name = input("Введите имя файла: ")
extension = get_file_extension(file_name)

if extension:
    print(f"Расширение файла: {extension}")

```

```java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Введите имя файла: ");
        String fileName = scanner.nextLine();

        try {
            String extension = getFileExtension(fileName);
            System.out.println("Расширение файла: " + extension);
        } catch (IllegalArgumentException e) {
            System.out.println("Невозможно определить расширение файла: " + e.getMessage());
        }
    }

    public static String getFileExtension(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1 || dotIndex == fileName.length() - 1) {
            throw new IllegalArgumentException("Отсутствует или некорректное расширение файла");
        }
        return fileName.substring(dotIndex + 1);
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kaze.gitbook.io/qa-theory/kodovye-zadachi/napishite-programmu-kotoraya-prinimaet-imya-faila-i-vyvodit-ego-rasshirenie.-esli-rasshirenie-u-fail.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
