الفرق بين المراجعتين ل"Python/Examples"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
ط
سطر 44: سطر 44:
 
     (result, consumed) = self._buffer_decode(data, self.errors, final)
 
     (result, consumed) = self._buffer_decode(data, self.errors, final)
 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 28: invalid continuation byte
 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 28: invalid continuation byte
 +
</syntaxhighlight>
 +
 +
=== الاستثناء <code>ValueError</code> ===
 +
<syntaxhighlight lang="python3">
 +
>>> f=open('D:\\test.txt','w')
 +
 +
>>> f.close()
 +
 +
>>> print('Hello World!' , file=f)
 +
Traceback (most recent call last):
 +
  File "<pyshell#8>", line 1, in <module>
 +
    print('Hello World!' , file=f)
 +
ValueError: I/O operation on closed file.
 
</syntaxhighlight>
 
</syntaxhighlight>

مراجعة 08:08، 24 أغسطس 2018

هذه الصفحة هي تجميع منظم ومرتب لأمثلة بلغة بايثون

الاستثناءات

تحتوي هذه الفقرة على أمثلة يصدر فيها استثناءات.

الاستثناء FileNotFoundError

تُصدره بعض التوابع القارئة للملفات عند عدم وجود الملف

>>> f=open('D:\\file.txt')
		      
Traceback (most recent call last):
  File "<pyshell#123>", line 1, in <module>
    f=open('D:\\file.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'D:\\file.txt'

الاستثناء PermissionError

يتم رميه عند محاولة الوصول إلى ملف، أو إنشاء ملف أو حذف ملف، دون أن يكون لمفسر بايثون الصلاحيات بالقيام بذلك

>>> open('C:\\file.txt','w')
		      
Traceback (most recent call last):


  File "<pyshell#105>", line 1, in <module>
    open('C:\\file.txt','w')
PermissionError: [Errno 13] Permission denied: 'C:\\file.txt'

الاستثناء UnicodeDecodeError

تحدث عند وجود كتابة بلغة غير الإنكليزية (باستخدام ترميز Unicode) وذلك عندما تستخدم تابع يتوقع قراءة ملفات بالإنكليزية حصرًا (أو بترميز utf-8)

في المثال التالي، فإن توابع الوحدة linecache مصممة لقراءة الملفات المصدرية لوحدات بايثون، وهي ملفات بالإنكليزية حصرًا.

>>> linecache.getline('D:\\file.txt',1)
		      
Traceback (most recent call last):
  File "<pyshell#116>", line 1, in <module>
    linecache.getline('D:\\file.txt',1)
  File "D:\Downloads\WinPython\python-3.6.5.amd64\lib\linecache.py", line 16, in getline
    lines = getlines(filename, module_globals)
  File "D:\Downloads\WinPython\python-3.6.5.amd64\lib\linecache.py", line 47, in getlines
    return updatecache(filename, module_globals)
  File "D:\Downloads\WinPython\python-3.6.5.amd64\lib\linecache.py", line 137, in updatecache
    lines = fp.readlines()
  File "D:\Downloads\WinPython\python-3.6.5.amd64\lib\codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 28: invalid continuation byte

الاستثناء ValueError

>>> f=open('D:\\test.txt','w')

>>> f.close()

>>> print('Hello World!' , file=f)
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    print('Hello World!' , file=f)
ValueError: I/O operation on closed file.