SORU
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ş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • 3DS Max Tutorials

    3DS Max Tuto

    4 AĞUSTOS 2013
  • EmbarkToHeaven

    EmbarkToHeav

    3 EYLÜL 2007
  • MuscleProdigyTV

    MuscleProdig

    8 Ocak 2011