TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun ยท 77.8K subscribers
Post #4477 2.84K
This adds new content without removing existing data.

๐Ÿ”น 10. Using the "with" Statement โญ

The recommended way to work with files is by using the "with" statement.

It automatically closes the file after use.
with open("sample.txt", "r") as file:
    print(file.read())

You don't need to call
close()

manually.

๐Ÿ”น 11. Reading a File Line by Line
with open("sample.txt", "r") as file:
    for line in file:
        print(line.strip())

This is useful for processing large files efficiently.

๐Ÿ”น 12. Real-World Data Science Example

Suppose you have a text file containing sales data:
100
250
175
300

Python code:
total = 0
with open("sales.txt", "r") as file:
    for line in file:
        total += int(line)
print(total)

Output:
825

In real-world projects, similar logic is used to process datasets before loading them into Pandas.

๐Ÿ”น 13. Common Mistakes 

โŒ Forgetting to Close the File
file = open("sample.txt", "r")
print(file.read())

Always use:
with open("sample.txt", "r") as file:
    print(file.read())

โŒ Opening a Non-Existent File
open("data.txt", "r")

If the file doesn't exist, Python raises a
FileNotFoundError

.

Always verify that the file exists or handle exceptions appropriately.

๐ŸŽฏ Practice Questions 

1. Create your own Python module with two functions and import it into another file. 

2. Import the "math" module and calculate the square root of 144. 

3. Create a text file and write five lines into it. 

4. Read a text file line by line using the "with" statement. 

5. Read a file containing numbers and calculate their average. 

๐ŸŽฏ Key Takeaways

โœ… A module is a reusable Python file containing code.

โœ… A package is a collection of related modules.

โœ… Use "import" to access modules and their functions.

โœ… Use "open()" to read and write files.

โœ… Prefer the "with" statement because it automatically closes files.

โœ… File handling is a fundamental skill for reading datasets, logs, configuration files, and other real-world data sources. 

Mastering modules, packages, and file handling will prepare you for working with Python libraries like Pandas, NumPy, and Scikit-learn, where data is frequently loaded from external files.

Double Tap โค๏ธ For More
  • โค 9
  • ๐Ÿ‘ 1
More from @datasciencefun
  1. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 โ€” Forgetting that LIMIT applies after sorting Forโ€ฆ
  2. Oct 7, 2026๐Ÿš€ Data Science Roadmap 2026 ๐Ÿ“ Phase 3: SQL for Data Science ๐Ÿ“– Topic 4 โ€” LIMIT LIMIT isโ€ฆ
  3. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  4. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  5. Oct 2, 2026Data Visualisation tips for beginners
  6. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’