Python फाइल हैंडलिंग क्विज: समर्पित गाइड
12 Questions
1 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Python में फ़ाइल खोलने के लिए कौन-सा फ़ंक्शन इस्तेमाल होता है?

  • `open()` (correct)
  • `read()`
  • `write()`
  • `close()`
  • Python में फ़ाइल को किस तरह से खोला जाता है?

  • file_open = open('file_name', 'read')
  • file_open = open('file_name', 'write')
  • file_open = open('file_name', 'w')
  • file_open = open('file_name', 'r') (correct)
  • Python में फ़ाइल से डेटा पढ़ने के लिए कौन-सा मोड उपयोगी है?

  • `w+`
  • `rb+`
  • `a+`
  • `r+` (correct)
  • Python में फ़ाइल को बंद करने के लिए कौन-सा फ़ंक्शन है?

    <p><code>close()</code></p> Signup and view all the answers

    Python में फ़ाइल को कौन-से mode में खोलकर आप binary data को read और write कर सकते हैं?

    <p>rb+</p> Signup and view all the answers

    Python में फ़ाइल को read करने के लिए कौन-सा method इस्तेमाल किया जाता है?

    <p>read()</p> Signup and view all the answers

    Python में फ़ाइल को write करने के लिए कौन-सा method इस्तेमाल किया जाता है?

    <p>write()</p> Signup and view all the answers

    Python में फ़ाइल को append करने के लिए कौन-सा mode इस्तेमाल किया जाता है?

    <p>a</p> Signup and view all the answers

    Python में फ़ाइल create करने के लिए कौन-सा mode इस्तेमाल किया जाता है?

    <p>c</p> Signup and view all the answers

    Python में context managers (with statements) का प्रयोग file handling में क्यों किया जाता है?

    <p>फाइल close होती है स्वत:म</p> Signup and view all the answers

    Python में file open करने से पहले, file exists की जाँच क्यों important है?

    <p>Exceptions handle करने में मददगार होती है</p> Signup and view all the answers

    'rb+' mode में file open करने से binary data read/write करने में क्या advantage होता है?

    <p>'rb+' mode allows binary data read/write without any conversion</p> Signup and view all the answers

    Study Notes

    Python File Handling: A Comprehensive Guide

    File handling in Python is essential for persisting data after a program terminates, as it allows data to be saved to a file and accessed later. Python provides several built-in functions and modules for handling files, making it easy for developers to work with files in various ways. In this article, we will explore the basics of file handling in Python, including opening, reading, writing, and closing files.

    Opening Files in Python

    To open a file in Python, you can use the built-in open() function. The basic syntax is:

    file_object = open('file_name', 'mode')
    

    The file_name includes the file extension and assumes the file is in the current working directory. If the file location is elsewhere, provide the absolute or relative path. The mode is an optional parameter that defines the file opening method.

    There are six access modes available in Python for files:

    1. Read (r): Opens the file for reading, allowing you to read the content but not edit or delete any content.
    2. Write (w): Opens the file for writing, allowing you to create a new file or overwrite an existing one.
    3. Append (a): Opens the file for appending, allowing you to add content to an existing file.
    4. Update (u): Opens the file for updating, allowing you to read, write, or append content.
    5. Create (c): Opens the file for creating, allowing you to create a new file.
    6. Binary read (rb+): Opens the file in binary mode, allowing you to read and write binary data.

    Reading and Writing Files in Python

    Once the file is opened, you can perform various file operations, such as reading and writing. To read a file, you can use the read() method, which returns the entire content of the file as a single string. To write a file, you can use the write() method, which appends or overwrites content in the file.

    ## Read a file
    with open("example.txt", "r") as file:
        content = file.read()
        print(content)
    
    ## Write a file
    with open("example.txt", "w") as file:
        file.write("This is a new file.")
    

    File Handling Best Practices

    When working with files in Python, it's essential to follow best practices to ensure that your code is secure, reliable, and performs well. Some best practices include:

    • Use context managers (with statements) to automatically close the file after reading or writing.
    • Check if the file exists before opening it, and handle exceptions accordingly.
    • Use appropriate access modes for reading and writing, such as r, w, a, or r+.

    Conclusion

    File handling in Python is a crucial skill for any developer. By understanding how to open, read, write, and close files, you can effectively store and retrieve data in Python applications. Remember to follow best practices when working with files to ensure secure and reliable code.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    यह क्विज Python में फाइल हैंडलिंग के मौलिक सिद्धांतों, फाइल खोलने, पढ़ने, लिखने, और बंद करने की प्रक्रिया को समझाता है। यह सामग्री Python में फाइलों के साथ काम करने के तरीकों को समझने में मदद कर सकती है।

    More Like This

    Python File Handling Quiz
    5 questions

    Python File Handling Quiz

    EvaluativeEnlightenment avatar
    EvaluativeEnlightenment
    Untitled
    10 questions

    Untitled

    SmoothestChalcedony avatar
    SmoothestChalcedony
    Use Quizgecko on...
    Browser
    Browser