Найти - Пользователи
Полная версия: автоматический контроль дискового пространства
Начало » Python для новичков » автоматический контроль дискового пространства
1
ijj
Всем привет! По примеру пишу мой первый скриптик, но не могу дописать в виду возникающей ошибки

Посдкажите как исправить ошибку (ValueError: need more than 1 value to unpack) в данной ситуации?


#!/usr/local/bin/python

import os
df_info=os.popen(“df -k /usr |grep -E ^/dev | awk ‘{ print $1, $4 }’”)
format_df=df_info.read().split()
df_info.close()
allow_value=2000000 #2G of disk space

for cycle in format_df:
dev, free=cycle.split()
if int(free)< allow_value:
print ‘Disk space is lower then 2G’ #you can mail this

————————————————————–
# /root/python_scripts/test2.py
Traceback (most recent call last):
File “/root/python_scripts/test2.py”, line 10, in <module>
dev, free=cycle.split()
ValueError: need more than 1 value to unpack
Vader
Это значит, что cycle.split() возвращает только одно значение, а вы пытаетесь передать его в 2 переменных dev и free.
Исправить можно обернув в cycle.split() в try/except.
o7412369815963
вместо os.popen лучше использовать subprocess: пример
ijj
Vader
Это значит, что cycle.split() возвращает только одно значение, а вы пытаетесь передать его в 2 переменных dev и free.
Исправить можно обернув в cycle.split() в try/except.
Посмотрел разные примеры использования исключений и сделал своё, но почему то интепретатор ругается на except , не могу понять пчиму (

#!/usr/local/bin/python

import os
df_info=os.popen(“df -k /usr |grep -E ^/dev | awk ‘{ print $1, $4 }’”)
format_df=df_info.read().split()
df_info.close()
allow_value=2000000 #2G of disk space
for cycle in format_df:
try:
dev, free=cycle.split()
if int(free)< allow_value:
except ValueError:
print ‘Disk space is lower then 2G’ #you can mail this



———————————

/root/python_scripts/test2.py: 13 lines, 372 characters.
# /root/python_scripts/test2.py
File “/root/python_scripts/test2.py”, line 12
except ValueError:
^
SyntaxError: invalid syntax
pill
ijj
for cycle in format_df:
try:
dev, free=cycle.split()
if int(free)< allow_value:
except ValueError:
print 'Disk space is lower then 2G' #you can mail this
try-except должны быть на одном уровне.
http://docs.python.org/tutorial/errors.html#handling-exceptions

PS:Но мне кажется проблема не здесь. Если что - покажете содержимое df_info, может что-т подскажется.
ijj
pill
ijj
for cycle in format_df:
try:
dev, free=cycle.split()
if int(free)< allow_value:
except ValueError:
print 'Disk space is lower then 2G' #you can mail this
try-except должны быть на одном уровне.
http://docs.python.org/tutorial/errors.html#handling-exceptions

PS:Но мне кажется проблема не здесь. Если что - покажете содержимое df_info, может что-т подскажется.
У меня ведь except идет после if а там тоже отступ нужен вот и получилось видимо что не на одном уровне. Сделал на одном уровне отругался про отступ

File “/root/python_scripts/test2.py”, line 12
except ValueError:
^
IndentationError: expected an indented block

вывод переменно df_info

<open file ‘df -k /usr |grep -E ^/dev | awk ’{ print $1, $4 }'', mode ‘r’ at 0x2834ef08>


вывод переменной format_df :

test2.py: 8 lines, 210 characters.
# /root/python_scripts/test2.py
pill
Сорри - проморгал )
for cycle in format_df:
try:
dev, free=cycle.split()
if int(free)< allow_value:
print 'Disk space is lower then 2G' #you can mail this
except ValueError:
pass


Если format_df = , зачем вам цикл, и что вы пытаетесь сделать через split?
...
dev, free = format_df
if int(free)< allow_value:
print 'Disk space is lower then 2G' #you can mail this
ijj
pill
Сорри - проморгал )
for cycle in format_df:
try:
dev, free=cycle.split()
if int(free)< allow_value:
print 'Disk space is lower then 2G' #you can mail this
except ValueError:
pass


Если format_df = , зачем вам цикл, и что вы пытаетесь сделать через split?
...
dev, free = format_df
if int(free)< allow_value:
print 'Disk space is lower then 2G' #you can mail this
И то верно.. все работает. без цикла for . Спасибо большое за помощь!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB