Posts

Showing posts from September, 2020

55.Programming Assignment 12-using Files

1.Input list of strings and create a text file with this sequence. 2.Copy one file to another ( text and binary ( image)) 3.Copy one python program file to another after removing the single line comments ( lines starting with #) 4.Read and print a file with line numbers of each line. 5.Read and print a file with all punctuation removed.( university question) 6.Write a python program to create a file with 10 integer numbers. Read this file and display the square of each number. (University question) 7.Write a program that reads a file and writes out a file with the lines in reversed order.( university question-refer example) 8.Write a python program to read a text file, copy the content to another file after removing the blank lines.( university question) 9.Count the number of lines, number of words and number of characters in a file. 10.Program to read a text file and print the words and its length in sorted order.( refer example) 11.Write a Python program that opens a file for input a...

54.Example Programs using Files

Write Python code for the following statements ( University question) i)writes the text ”PROGRAMMING IN PYTHON” to a file with name code.txt ii) then reads the text again and prints it to the screen. f=open('code.txt','w') f.write("PROGRAMMING IN PYTHON") f.close() f=open('code.txt','r') s=f.read() print(s) f.close() Copy file1.dat to file2.dat try:      f1 = open("sample.dat", "r")      f2 = open("sample3.dat", "w")      content=f1.read()      f2.write(content)      f1.close()      f2.close()      print('File is copied') except:      print("file does not exist") #copying a binary file ( image) try:      f1 = open("cat.jpg", "rb")      f2 = open("catcopy.jpg", "wb")      content=f1.read()      f2.write(content)      f1.close()      f2.close()      print('File is cop...

53.File Handling in Python- text and binary files

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc).  When there are a large number of files, they are often organized into directories (also called 'folders'). Each file is identified by a unique name, or a combination of a file name and a directory name.  By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. Hen...

52. The os and sys module

Image
The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. This os and os.path modules include many functions to interact with the file system. Following are some functions in OS module: 1. os.name This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’ >>>import os >>>os.name 'nt' 2.os.getcwd()  Function os.getcwd(), returns the Current Working Directory(CWD) of the file used to execute the code, can vary from system to system. >>> os.getcwd() 'C:\\Users\\binuvp\\AppData\\Local\\Programs\\Python\\Python38-32' 3.os.listdir('.') . To print files and directories in the current directory  on your system  >>> os.listdir('.') ['are.py...

51.Exception Handling in Python

We can make certain mistakes while writing a program that lead to errors when we try to run it. A python program terminates as soon as it encounters an unhandled error. These errors can be broadly classified into two classes: Syntax errors Logical errors (Exceptions) Python Syntax Errors Error caused by not following the proper structure (syntax) of the language is called syntax error or parsing error. Let's look at one example: >>> if a < 3   File "<interactive input>", line 1 if a < 3  ^  SyntaxError: invalid syntax As shown in the example, an arrow indicates where the parser ran into the syntax error.We can notice here that a colon : is missing in the if statement. Python Logical Errors (Exceptions) Errors that occur at runtime (after passing the syntax test) are called exceptions or logical errors. For instance, they occur when we try to open a file(for reading) that does not exist (FileNotFoundError), try to divide a number by zero (ZeroDiv...