Email validation
#Email validator
import re
def is_email():
email=input("Enter your email")
pattern = '[\.\w]{1,}[@]\w+[.]\w+'
file = open('ValidEmails.txt','r')
if re.match(pattern, email):
file.write(email)
I am wondering why my data wont write to the disk. Python says that my operation is not supported.
is_email
file.write(email)
io.UnsupportedOperation: not writable
asked Dec 3, 2014 at 18:13
0
You open the variable «file» as a read only then attempt to write to it:
file = open('ValidEmails.txt','r')
Instead, use the ‘w’ flag.
file = open('ValidEmails.txt','w')
...
file.write(email)
answered Dec 3, 2014 at 18:17
triphooktriphook
2,9153 gold badges25 silver badges35 bronze badges
0
file = open('ValidEmails.txt','wb')
file.write(email.encode('utf-8', 'ignore'))
This is solve your encode error also.
answered Aug 30, 2017 at 12:48
Anurag MisraAnurag Misra
1,51618 silver badges24 bronze badges
use this :
#Email validator
import re
def is_email():
email=input("Enter your email")
pattern = '[\.\w]{1,}[@]\w+[.]\w+'
file = open('ValidEmails.txt','w')
if re.match(pattern, email):
file.write(email)
answered Apr 5 at 19:44
The write operation comes under file handling in Python. File handling is an integral part of Python. It includes performing various file-related operations. One of the most commonly used are the read and write operations, and one of the most widely occuring errors is the ‘io.unsupportedoperation: not writable’ error.
Many things can cause an error in Python, but one of the most common errors that Python programmers face is an IO operation (input/output) that is not supported.
Have you ever wondered what will happen in case you write to a file opened in read mode? Well, the title explains it well. Let us understand the ‘io.unsupportedoperation: not writable’ error in detail. In this article, we will go over the different ways to fix this error and why it happens.
What are IO operations?
In Python, io.supported_operations is a dictionary of the supported operations for each I/O operation.
The difference between these two operations is that I/O is used to read or write data from or to a file, while supported_operations indicates whether or not an operation is supported by the interpreter.
Also, IO operations are used to read and write data from a file or device. They are supported only for files. This statement means that if you want to read from or write to some other destination, you will need to use other methods.
These operations can be classified as text I/O, binary I/O, and raw I/O. There is a device error, also. Due to this, windows are not allowed to perform any read or write operation and extract data.
Understanding the io.unsupportedoperation: not writable error
When we don’t specify any mode, the file opens in read mode by default. Now, look at the following piece of code:
with open('pythonfile.txt', 'r') as f:
f.write('FIRST WORD')
Here, a programmer has opened file in read mode. In the next statement, he is trying to write a string to the file. This will result in the given error i.e. IO unsupported error.
Also, this can happen if you have a directory in your path that has spaces in it, or if you try to write to an existing file.
See also: [Solved] ValueError: I/O operation on closed file. (Opens in a new browser tab)
Solution of the io.unsupportedoperation: not writable error
In order to solve the error, you need to change the mode or read the file only. The mode can be changed into either write or append. The write mode deletes previously added data while append mode will add the string to the location where end of line character is present. Corrected code will look like this:
with open('pythonfile.txt', 'a') as f:
f.write('FIRST WORD')
Otherwise we can perform read and write together using r+b mode. utf-8 implies encoding the byte since you want to open file in binary mode.
with open('pythonfile.txt', 'r+b') as f:
f.write(bytes('FIRST WORD', 'utf-8'))
The simplest way to fix an IO operation that isn’t supported is simply by using another method of input or output: print(“hello world”) or io.open(“/etc/passwd”, “r”).read().
However, this isn’t always ideal: if your code relies on input being available through the open() call and it fails, then it’s going to fail at runtime too! The best way to handle these situations is by checking whether your function supports IO operations before trying to use them. The easiest way to do this is via a try/except block: try: print(“hello world”) except NotSupportedError as e: print(“error”, e)
To sum it up, this error occurs when trying to write a file in the directory without having permission. The solution is simple: either add the necessary permission or change the directory name.
io.unsupportedoperation with json files
In case you are encountering the same error in a json file, the reason is the same. By default, open() method will open file in read mode. Hence, you need to change it to write mode to fix the error. Example:
#with error
with open("myfile.txt") as jsonfile:
json.dump(hero, jsonfile)
Initially, no mode was given so it couldn’t write. Now,after altering the code will become error free.
#without error
with open("myfile.txt", "w") as jsonfile:
json.dump(hero, jsonfile)
Python’s writable method for file handling
IObase class contains writable function in Python. It gives TRUE or FALSE output showing whether the file is writable or not. Example:
a = open("abc.txt", "r")
print(a.writable())
b = open("abc.txt", "a+")
print(b.writable())
So the output will be:
FALSETRUE #because opening the file in write or append mode will only make it writable. #otherwise we will get the ans as FALSE
io.unsupportedoperation with try and except block
You can also throw an exception yourself using try-except block in Python. The io module consists of this file handling error. So with the help of this module you can raise this exception. Follow the given code to get an understanding.
import io
try:
with open("myfile.txt", "w") as f:
print(f.read())
except io.UnsupportedOperation as e:
print(e)
Use the I/O tool to see if the data is writeable. This will let you know whether or not it can be written to. Or you can check that your data is comma-separated values (CSV) and not a fixed-width file (like a text file) by using I/O again and seeing if it says “UTF-8 encoded string.”
io.unsupportedoperation: not writable yaml dump
It seems that your yaml dump is not writable, which means it’s not a valid YAML dump. io.unsupportedoperation: not writable yaml dump. Also, the io.unsupportedoperation error occurs when you try to write a yaml dump file, but you’re not running in the correct environment and/or you don’t have the necessary permissions.
To fix this problem, please run the following command: sudo shutdown -h now
You can also try changing the working directory of your YAML dump file from /tmp/yaml_dump to your home directory. This is done by using the chmod command with no arguments. If that doesn’t work, try using sudo to run your command as root or using su to become root. You can do this by adding a sudo prefix before any commands that need root privileges.
See Also: [Fixed] “io.unsupportedoperation not readable” error
FAQs
How to check if a file exists before reading it?
We can use os.path.exists() method to check whether the path exists or not.
What is the output of writable function in Python?
It gives boolean output in the form of True and False. In case the file is writable you will get True as your answer. However, if the file is not opened in write mode or append mode, you will get False as the answer.
What does unsupported operation mean in Python?
Unsupported operation is the term used to describe a function or operator that is not supported by the Python interpreter. They are often used in conjunction with a non-standard library. They can lead to unexpected behavior or crashes and should be avoided when possible. Arithmetic and comparison operators are often unsupported, so it is important to use other ways of expressing the same operation.
How do you read and write the same file in Python
Opening the file in r+ mode helps to access both read and write operations in Python.
Conclusion
We discussed the meaning of IO operations in Python and their classifications. You must have learned how to fix the io.unsupportedoperation: not writable error. Apart from this, we have discussed the alternatives you can approach to make the code error free while handling files in Python and using writable keywords while performing file-handling operations.
-
[Solved] typeerror: unsupported format string passed to list.__format__
●May 31, 2023
-
Solving ‘Remote End Closed Connection’ in Python!
by Namrata Gulati●May 31, 2023
-
[Fixing] Invalid ISOformat Strings in Python!
by Namrata Gulati●May 31, 2023
-
Frustrated with Unmatched f-strings? Master the Art of Formatting
by Namrata Gulati●May 30, 2023
Я пишу код python в редакторе NotePad++
вот код:
file = open( ‘D:/test.txt’ )
file.write( ‘Что то’ )
и получаю ошибку:
Traceback (most recent call last):
File «D:\p.py», line 3, in
file.write( ‘Что то’ )
io.UnsupportedOperation: not writable
Я в програмировании новичок, подскажите где ошибка
-
Вопрос задан
-
12382 просмотра
Попробуйте:
file = open("D:/test.txt","w")
Аргумент «w» нужен для перезаписи файла (весь текст пишется заново), «a» нужен если нужно дописать текст в файле (текст добавляется в конец файла)
PS: Если нужно прочитать файл, то:
file = open("D:/файл.txt","r")
text = file.read()
Надеюсь, я помог вам.
Пригласить эксперта
-
Показать ещё
Загружается…
21 сент. 2023, в 14:51
30000 руб./за проект
21 сент. 2023, в 14:49
25000 руб./за проект
21 сент. 2023, в 14:33
5000 руб./за проект
Минуточку внимания
Email validation
#Email validator
import re
def is_email():
email=input("Enter your email")
pattern = '[\.\w]{1,}[@]\w+[.]\w+'
file = open('ValidEmails.txt','r')
if re.match(pattern, email):
file.write(email)
I am wondering why my data wont write to the disk. Python says that my operation is not supported.
is_email
file.write(email)
io.UnsupportedOperation: not writable
asked Dec 3, 2014 at 18:13
0
You open the variable «file» as a read only then attempt to write to it:
file = open('ValidEmails.txt','r')
Instead, use the ‘w’ flag.
file = open('ValidEmails.txt','w')
...
file.write(email)
answered Dec 3, 2014 at 18:17
triphooktriphook
2,9153 gold badges25 silver badges35 bronze badges
0
file = open('ValidEmails.txt','wb')
file.write(email.encode('utf-8', 'ignore'))
This is solve your encode error also.
answered Aug 30, 2017 at 12:48
Anurag MisraAnurag Misra
1,51618 silver badges24 bronze badges
use this :
#Email validator
import re
def is_email():
email=input("Enter your email")
pattern = '[\.\w]{1,}[@]\w+[.]\w+'
file = open('ValidEmails.txt','w')
if re.match(pattern, email):
file.write(email)
answered Apr 5 at 19:44
- Fix the
io.UnsupportedOperation: not writableError in Python - Conclusion

Python is very efficient in reading and writing data from files. It has a variety of functions to help in file handling.
The basics of file handling involve opening a file using the open() function and reading or writing data based on the file mode.
The open() opens a given file and creates a file object that can be used to perform reading and writing operations on a file.
The file can be opened in different types of modes. By default, it opens the file in read mode.
This tutorial will discuss the io.UnsupportedOperation: not writable error in Python and ways to fix it.
Fix the io.UnsupportedOperation: not writable Error in Python
This error is caused when we try to perform the write operation on a file opened in reading mode. A file opened in read mode can only read the contents.
For example:
with open('sample.txt', 'r') as f:
f.write('Text')
Output:
io.UnsupportedOperation: not writable
Note that in the above example, we open the file in r mode (read) and try to write some data to this file using the write() function, which causes the error.
Remember to open the file in modes that support this operation to solve this. The write (w) or append (a) modes are used to write some data to a file.
The previous contents are truncated if we open the file in w mode. The a mode adds content to the end of the file and preserves the previous data.
For example:
with open('sample.txt', 'w') as f:
f.write('Text')
In the above example, we successfully avoid errors and can write data to the file.
If we want to simultaneously read and write data from a file, we can use the r+b mode. We can perform reading and writing operations in binary mode when the file is opened in this mode.
For example:
with open('sample.txt', 'r+b') as f:
f.write(bytes('Text', 'utf-8'))
Note that we write data as bytes since the file is opened in binary mode. The text is encoded as bytes in the utf-8 encoding in the above example.
Alternatively, we can also use the writable() function to check whether we can perform writing operations using the file handle or not. It returns True or False.
See the code below.
with open('sample.txt', 'r') as f:
print(f.writable())
with open('sample.txt', 'a') as f:
print(f.writable())
Output:
The above example shows that the writable function returns False when the file is opened in r mode and returns True when the file is opened in a mode.
Conclusion
To conclude, we discussed the cause behind the io.UnsupportedOperation: not writable error and how to fix it. We discussed how opening the file in the wrong mode can cause this and what file modes support writing operations.
We also demonstrated the use of the writable function that can be used to check whether a file object can perform writing operations or not.

![[Solved] typeerror: unsupported format string passed to list.__format__](https://www.pythonpool.com/wp-content/uploads/2023/05/typeerror-unsupported-format-string-passed-to-list.__format__-300x157.webp)

![[Fixing] Invalid ISOformat Strings in Python!](https://www.pythonpool.com/wp-content/uploads/2023/05/invalid-isoformat-string-300x157.webp)
