Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint
Cover Image
Cover Image
Drag to reposition
Contributed Answers

What does "print >>" do in python?

What does the statement "print >>" do in python?  
Answered on 21st Dec, 2017, 0 Views

"print >>" is a syntax to extend the standard 'print' statement so that it can be used to print to any file-like object, instead of the default sys.stdout. So it can be used to print directly to files. For example, you open a file called my_file, then you can write to it using: ,   This will write "HelloWorld" to my_file. It can also be used with other file like objects.

What does open() function do in Python?

What is the functionality of open() function in python?
Answered on 21st Dec, 2017, 0 Views

The function open() opens a file. You can use it like:  ,  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 second one is the open mode. It determines how the file gets opened, for example,  – If you want to read the file,pass in r – If .....

What does close() function do in Python?

What is the functionality of close function in python?
Answered on 21st Dec, 2017, 0 Views

The function close() closes an open file. For example:  ,  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 the file. You need to close it once you are done using the file. If your program encounters an error and doesn't call f.clos.....

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

What is difference between raw_input() and input() functions in Python?
Answered on 21st Dec, 2017, 0 Views

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, , This differs from input() in that the latter tries to interpret the input given by the user; it is usually best to avoid input() and to stick with raw_input() and custom parsing/conversion code. In Python 3.....

How to work with a text file in Python?

How do I work with a text file in python?
Answered on 21st Dec, 2017, 0 Views

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 PDF. The main difference between a text file and a binary file is that binary files need special pro.....

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

How do I read a text file into a list or array in python?
Answered on 21st Dec, 2017, 0 Views

, 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 and store it in a list: ,

How to open a file in binary mode with Python?

How do I open a file in binary mode in python?
Answered on 21st Dec, 2017, 0 Views

"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default. To open files in binary mode, when specifying a mode, add 'b' to it.For example, ,  Above code opens my_file.mp3 in binary r.....

How to open a binary file in append mode with Python?

How do I open a binary file in append mode in Python?
Answered on 21st Dec, 2017, 0 Views

"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like GIFs, audio files like MP3s or binary document formats like Word or PDF. To open files in binary append mode, when specifying a mode, add 'ab' to it. For example, , Above code opens my_file.mp3 in binary append mode and stores the file content in file_content vari.....

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

How to open a file in read and write mode with Python?
Answered on 21st Dec, 2017, 0 Views

To open files in read/write mode, specify 'w+' as the mode. For example, ,  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 as it doesn't truncate the file. 

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

How do I open a binary file in read and write mode in python?
Answered on 21st Dec, 2017, 0 Views

To open binary files in binary read/write mode, specify 'w+b' as the mode(w=write, b=binary). For example, , 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 also use r+mode as it doesn't truncate the file. 

1 2 3 4 5 6 7 Next
loader
Advertisements
Contribution

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.