2011-03-09 4 views
5

के माध्यम से विंडोज सीएमडी आदेश चलाएं मैं बड़ी निर्देशिका संरचना में सभी फ़ाइलों के लिए सिम्लिंक के साथ एक फ़ोल्डर बनाना चाहता हूं। मैंने पहले subprocess.call(["cmd", "/C", "mklink", linkname, filename]) का उपयोग किया, और यह काम किया, लेकिन प्रत्येक सिम्लिंक के लिए एक नई कमांड विंडो खोली।पायथन

मैं यह पता लगाने सकता है नहीं अप पॉपिंग एक खिड़की के बिना पृष्ठभूमि में आदेश को चलाने के लिए कैसे, इसलिए मैं अब एक अध्यक्ष एवं प्रबंध निदेशक खिड़की खुली रखने के लिए कोशिश कर रहा हूँ और stdin के माध्यम से वहाँ कमांड चलाने:

def makelink(fullname, targetfolder, cmdprocess): 
    linkname = os.path.join(targetfolder, re.sub(r"[\/\\\:\*\?\"\<\>\|]", "-", fullname)) 
    if not os.path.exists(linkname): 
     try: 
      os.remove(linkname) 
      print("Invalid symlink removed:", linkname) 
     except: pass 
    if not os.path.exists(linkname): 
     cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n") 

File "mypythonfile.py", line 181, in makelink 
cmdprocess.stdin.write("mklink " + linkname + " " + fullname + "\r\n") 
TypeError: 'str' does not support the buffer interface 

क्या मतलब है और कैसे: जहां

cmdprocess = subprocess.Popen("cmd", 
           stdin = subprocess.PIPE, 
           stdout = subprocess.PIPE, 
           stderr = subprocess.PIPE) 

हालांकि, मैं अब इस त्रुटि मिलती है क्या मैं इसे हल कर सकता हूं?

उत्तर

1

पायथन स्ट्रिंग यूनिकोड हैं, लेकिन जिस पाइप को आप लिख रहे हैं केवल बाइट्स का समर्थन करता है। आज़माएं:

cmdprocess.stdin.write(("mklink " + linkname + " " + fullname + "\r\n").encode("utf-8")) 
+0

आह। वह था, धन्यवाद। अब यह पूरी बात सिर्फ 10 या तो फाइलों के बाद काम करना बंद कर देती है ... शायद आप इसके लिए कुछ भी जानते हैं? मैंने http://stackoverflow.com/questions/5253835/yet-another-python-windows-cmd-mklink-problem-cant-get-it-to-work THX पर एक नया प्रश्न बनाया –