Python Class 18 - File Handling in Python

preview_player
Показать описание
File handling in Python, Python open Function, Python IO, Create a File in Python, Read a Text File, Write data to a file, and Delete a File.

Python IO – File Handling
Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files and Python treats files differently as text or binary.

We use open () function in Python to open a file in read or write mode. open () will return a file object, to return a file object we use open() function along with two arguments, that accept file name and the mode, whether to read or write.

Python Complete Tutorial

Python Videos PlayList

Python Programming Syllabus

Python Programming Quiz

Python Interview Questions for Fresher
--------------------------------------------------
1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures - Lists

15. Python Data Structures - Sets

16. Python Data Structures - Tuples

17. Python Data Structures - Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction in Python
--------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Python Complete Tutorial

Python Videos PlayList

Python Programming Syllabus

Python Programming Quiz

Python Interview Questions for Fresher

1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures - Lists

15. Python Data Structures - Sets

16. Python Data Structures - Tuples

17. Python Data Structures - Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction in Python

gcreddy
Автор

Class Notes:
Python Class 18: File Handling in Python

Python Built-in Functions
1. abs() Function
2. round() Function
3. ord() Function
4. chr() Function
5. input() Function
6. int() Function
7. float() Function
8. str() Function
9. len() Function
10. type() Function
11. max() Function
12. min() Function
13. range() Function

Python Built-in Functions Continuation:

14. pow() Function

It returns the value of x to the poer of y (x^y)

Example:
x = pow(10, 3)
print(x) #1000

x = (10 ** 3)
print (x) #1000

x = (10 ** 3 ** 2)
print (x)

15. sum() Function

It returns a number, the sum of all items in an inerrable

Example:

val =[10, 20, 30.5, 40, 50.7]
x=sum(val)
print(x)

val =(10, 20, 30.5, 40, 50.7)
x=sum(val)
print(x)

val ={10, 20, 30.5, 40, 50.7}
x=sum(val)
print(x)

16. print() Function

It prints the specified message to the screen or other standard output.

Example:

a=10
b=20

print("Hello Python")
print(123)
print(12.34)
print(True)
print(a+b)
print ("Addition of a, b is: ", (a+b)) #Addition of a, b is: 30
print ("A value is: ", a, " B value is: ", b)#A value is: 10 B value is: 20


mylist = [10, 20, 30, 40, 50]
print (mylist)

mytuple = [10, 20.7, 30, 40, 50]
print (mytuple)

myset = {10, 20.7, 30, 40, 50}
print (mytuple)

mydict = {"Name": "Rajesh", "Age": 25, "City": "Hyderabad"}
print (mydict)

File Handling in Python

System Point of view: Everything is file

User Point of view: Drive, Folder, File (text/flat, doc, xls, ppt, pdf, etc), Database, etc,

open() Function for handling files

four modes for opening a file

"x" - Create
"r" - Read/Default
"w" - Write
"a" - Append

Examples:

1. Create a New File

open() Function and "x" mode

fileobj = open("C:/Users/gcreddy/Desktop/December.txt", "x")

fileobj = open("C:/Users/gcreddy/Desktop/December.doc", "x")

fileobj = open("C:/Users/gcreddy/Desktop/December.xls", "x")

fileobj = open("C:/Users/gcreddy/Desktop/December.pdf", "x")

Assignment: Check the existence of a file, if not exits then create the file.

2. Renaming a File

Import os module for renaming a file,

module/package/code library

Writing Python Program

Identifiers (class, object, function, varaible, etc, )
Keywords (if, for, while, else, elif, break, continue, return, def, try, etc, )
Built-in Functions (print, input, type, len, open, etc, )
Built-in methods
Data (10, 10.23, "India", etc, )

Built-in and User defined - Modules/Packages/Code libraries

External Modules/Packages (Excel module)

PSF

import os
os.rename("C:\\Users\\gcreddy\\Desktop\\December.txt",

import os
os.rename("2021.txt", "abcdef.txt")

3. Return the current directory

import os
x= os.getcwd()
print(x)

4. Change the current directory

import os
os.chdir("D:\\Dump")
x= os.getcwd()
print(x)

gcreddy