Manogna has Published 66 Answers

How to implement user defined exception in Python?

Manogna

Manogna

Updated on 04-Dec-2019 10:02:43

We create user-defined or custom exceptions by creating a new exception class in Python. The idea is to derive the custom exception class from the exception class. Most built-in exceptions use the same idea to enforce their exceptions.In the given code, you have created a user-defined exception class, the “CustomException“. ... Read More

What is difference between raw_input() and input() functions in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:42:24

The function raw_input() presents a prompt to the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string. For example, name = raw_input("What isyour name? ") print "Hello, %s." %nameThis differs from input() in that the latter tries ... Read More

How to work with a text file in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:41:52

A text file is any file containing only readable characters. The opposite of text files, "binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs orGIFs, audio files like MP3s or binary document formats like Word or ... Read More

How to read text file into a list or array with Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:41:08

f = open('my_file.txt', 'r+') my_file_data = f.read() f.close()The above code opens 'my_file.txt' in read mode then stores the data it reads from my_file.txt in my_file_data and closes the file. The read function reads the whole file at once. You can use the following to read the file line by line ... Read More

How to open a file in read and write mode with Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:39:18

To open files in read/write mode, specify 'w+' as the mode. For example, f = open('my_file.txt', 'w+') file_content = f.read() f.write('Hello World') f.close() Above code opens my_file.txt in write mode, stores the file content in file_content variable and rewrites the file to contain "Hello World". You can also use r+ mode ... Read More

How to open a binary file in read and write mode with Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:38:47

To open binary files in binary read/write mode, specify 'w+b' as the mode(w=write, b=binary). For example, f = open('my_file.mp3', 'w+b') file_content = f.read() f.write(b'Hello') f.close()Above code opens my_file.mp3 in binary read/write mode, stores the file content in file_content variable and rewrites the file to contain "Hello" in binary. You can ... Read More

What does open() function do in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:30:05

The function open() opens a file. You can use it like: f = open('my_file', 'r+') my_file_data = f.read() f.close() The above code opens 'my_file'in read mode then stores the data it reads from my_file in my_file_data and closes the file. The first argument of open is the name of the file and ... Read More

What does close() function do in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:29:19

The function close() closes an open file. For example: f = open('my_file', 'r+') my_file_data = f.read() f.close() The above code opens 'my_file'in read mode then stores the data it reads from my_file in my_file_data and closes the file. When you open a file, the operating system gives a file handle to read/write ... Read More

How do I find the location of Python module sources?

Manogna

Manogna

Updated on 30-Sep-2019 09:02:50

For a pure python module you can find the location of the source files by looking at the module.__file__. For example,  >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built in modules, however, are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and ... Read More

How to write a python module?

Manogna

Manogna

Updated on 30-Sep-2019 09:02:21

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py create helloworld.py then write the following function as its content: def hello_world():     print "Hello world" Now create another file in the same directory called script.py. Write the following as its ... Read More

Advertisements