Python is file writable

Python writable() Function

The writable() function in Python, used to check whether the file is writable or not. In other words, we can say that this function is used to check whether we can write to the file or not.

Note: The file may not be writable because of many reasons such as the write operation on the file is not allowed, or the file opening mode used as r (the reading only mode) etc.

Python writable() Syntax

The syntax to use writable() function is:

where fh indicates to file handler or object. It returns True if file whose handler is fh, is writable, otherwise returns False.

Python writable() Example

Let’s create an example program that uses writable() function to check and print the message regarding, whether the file is writable or not:

fh = open("codescracker.txt", "w") if fh.writable(): print("The file \"codescracker.txt\" is writable.") else: print("The file \"codescracker.txt\" is not writable.") fh.close()

python writable

Since the file codescracker.txt opened in w mode, therefore we can write to the file using its handler.

If you change the opening mode from w to r (reading only), then the output will be:

writable python

Note: If r mode is used while opening the file, then the open() function produces error or throws exception say FileNotFoundError. Here is an example:

try: fh = open("none.txt", "r") if fh.writable(): print("It is writable.") else: print("It is not writable.") except FileNotFoundError: print("File not found!")

Because the file none.txt does not available, therefore you’ll see the output:

Liked this article? Share it!

Источник

Python File writable() Method

This article will comprehend the utilization of the “writable()” method on different modes of behavior for opening a file.

Example#1: Utilizing the Python writable() Method on a File Open in Writing Mode

The Python file “writable()” method will be utilized in this illustration to check whether the specified file is writable or not when it is opened in the writing mode of behavior. We have created an example Python program for the implementation of this method which can be viewed in the snapshot below:

We will explain this Python code snippet here.

In this program, we have initially invoked the Python “input()” method. As the name indicates, this technique is employed to accept any input from the user. Between the function brackets of the “input()” method, we can pass an optional parameter which is to mention a prompt message that would be displayed on the terminal to the user while taking input. We have used this argument and wrote a text string “Insert the file name. The user will now get a window prompting them to input the file name. This method will seek the user input; thus, it needs a storage space where it can put this input. So, we created a variable “holder” that will hold the user-provided file name. Next, we need to put this file name on the screen to be viewable.

Here, the first “print()” method comes into practice. It will simply take the provided input and exhibit it on the Python console. We, in the following line, provided two inputs: a statement “The user has entered the file name:” and the variable holding the file name as “holder”, both separated by a comma.

The proceeding line of the code tells us that the program has now called the “open()” method to find the specified file and open it. The function will check in the CWD whether a file with the given name already appears. If the file exists, it will simply open it. On the other hand, if there is no such file, it will create the one with the user-provided name in the CWD. Between the braces of the function, we need to provide the file name and it also takes another optional argument which is the mode of behavior for that particular file. As the file name will be taken from the user at the time of execution and can be accessed by the variable “holder” so instead of the file name, we have passed the “holder” variable, and the mode of behavior is specified as “w”.

The opened file with its current mode will be saved in the “notes” file object. This signifies that the file will be opened in writing mode. Now, to write content to this file, we have invoked the file “write()” method with the file object “notes”. Within the parentheses, a text string is provided to be written into the file as “This is a new file.”

Now, it is time to introduce our topic of discussion, which is the “writable()” method, to this program. To check if the file is writable or not, we have called the “writable()” method with the file object “notes”. As a consequence, the function will either return a “Tue” or a “False” Boolean value. To hold the resultant value, a variable “catch” is generated. The “print()” method is then invoked to exhibit the result. We have passed it a text statement “Is the provided file writable?” and the variable storing the result “catch”. Lastly, the file is closed using the python “close()” method.

Here, in the snapshot provided above, we can see that the control is asking the user to insert the file name.

Once the user inserted the name “workbook.txt” and hit the “Enter” key, it will execute the remaining operations, which says that the file is writable.

Also, we can find out that the file, with the specified string line in it, created through the python program is present in the CWD of the python environment.

Example#2: Utilizing the Python writable() Method on a File Open in Append Mode

Another demonstration will be provided in this section where we will open an existing file in append mode and then apply the “writable()” method to verify if we can write on it or not. The following sample program is created to implement this technique:

The initial requirement here is to open the file. For this, we have invoked the file “open()” method. Within its function braces, the file name is specified along with the mode of behavior. The file we have provided is “workbook.txt” and the mode of behavior is “a”, which means we have opened a particular file in append mode. Now, to store this file in its current mode, we have created a file object “copy”. The file can be accessed throughout the program by mentioning this file object.

To append a string line to this file, we have invoked the “write()” method with the file object “copy()”. Between the function braces, the string line to be appended is specified as “\nWe are appending a new text line into this file.” In the next line of the script, the “writable()” method is called to check if the file opened in append mode is writable or not. A variable “mold” will hold the outcome generated from calling the “writable()” method. To put this outcome on display, we have utilized the python method “print()”. This method is invoked with two inputs; a statement “Is this file writable?” and the variable holding the result “mold”. Finally, the file is shut using the “close()” function.

As we execute the program, it gives us the output that can be viewed in the snapshot above. It concludes that a file in append mode is writable.

By opening the file, we come to know that the new text string is successfully appended.

Example#3: Utilizing the Python writable() Method on a File Open in Reading Mode

The last illustration will apply the “writable()” method on a file opened in the reading mode of behavior. The Python script given below will be used here.

In the beginning line of the program, the “open()” method is invoked. Between its braces, we have specified the file name as “workbook.txt” and the mode is specified as “r”. So, we are opening a specific file in the reading mode of behavior. To store this file in the program, we have created a file object “repository”. The “writable()” method is utilized to evaluate whether the file is writable or not with the file object “repository”. The result generated from this method will be stored in the variable “z”. Then, we passed the variable “z” to the “print()” method along with a text string “Is specified file writable?” to be displayed on the terminal. The “close()” function is utilized to shut down the file.

The result we get from executing the above block of code is that we cannot write on a file opened in reading mode.

Conclusion

Utilizing the python file “writable()” method is a useful technique. In this guide, we have learned to implement this method into our python programs. This guide is composed of three practical illustrations to find out which mode of behavior for opening a file allows us to write on that particular file. After performing these programs, we concluded that a file is writable in writing as well as append mode whereas the reading mode does not allow us to write on a file.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Источник

Читайте также:  Java switch and strings
Оцените статью