24 Mayıs 2011, Salı
Python olumsuzluk
Eğer bir yolu yok ise, ancak bir dizin oluşturmak için çalışıyorum. (değil) operatörü işe yaramıyor. Python inkar etmek nasıl emin değilim... Ne bu yapmak için doğru bir yolu var mı?
if (!os.path.exists("/usr/share/sounds/blues")):
proc = subprocess.Popen(["mkdir", "/usr/share/sounds/blues"])
proc.wait()
CEVAP
24 Mayıs 2011, Salı
Python olumsuzluk operatörü not
. Bu nedenle sadece ** 5 not
ile değiştirin.
Eğer, örneğin, bunu yapmak için:
if not os.path.exists("/usr/share/sounds/blues") :
proc = subprocess.Popen(["mkdir", "/usr/share/sounds/blues"])
proc.wait()
İçin spesifik bir örnek olarak Neil dedi yorum) gerek yok kullanmak için subprocess
modülü, kolay kullanım os.mkdir()
almak için neden ihtiyacın var, ilave özel durum işleme iyilik.
Örnek:
blues_sounds_path = "/usr/share/sounds/blues"
if not os.path.exists(blues_sounds_path):
try:
os.mkdir(blues_sounds_path)
except OSError:
# Handle the case where the directory could not be created.
Bunu Paylaş:
Demir Python kullanırken Asp.Net içind...
Python: uıd/gıd bulmak için, belirli b...
Python - virtualenv kullanarak el ile ...
Bulma Yerel IP adresleri'In Pytho...
Basit bir XML dosyası oluşturma kullan...