2009-04-22 9 views
5

के लिए मैं वर्तमान में निर्देशिका वॉकर से Hereनिर्देशिका वाकर अजगर

import os 
class DirectoryWalker: 
# a forward iterator that traverses a directory tree 

def __init__(self, directory): 
    self.stack = [directory] 
    self.files = [] 
    self.index = 0 

def __getitem__(self, index): 
    while 1: 
     try: 
      file = self.files[self.index] 
      self.index = self.index + 1 
     except IndexError: 
      # pop next directory from stack 
      self.directory = self.stack.pop() 
      self.files = os.listdir(self.directory) 
      self.index = 0 
     else: 
      # got a filename 
      fullname = os.path.join(self.directory, file) 
      if os.path.isdir(fullname) and not os.path.islink(fullname): 
       self.stack.append(fullname) 
      return fullname 

for file in DirectoryWalker(os.path.abspath('.')): 
    print file 

उपयोग कर रहा हूँ यह मामूली परिवर्तन आप फ़ाइल के भीतर पूरा पथ की अनुमति देता है।

क्या कोई मेरी मदद कर सकता है कि फ़ाइल फ़ाइल को कैसे ढूंढें और इसका उपयोग कैसे करें? मुझे पूरा पथ, और सिर्फ फ़ाइल नाम दोनों की आवश्यकता है।

+0

क्या कोई मेरी मदद कर सकता है कि फ़ाइल फ़ाइल को कैसे ढूंढें और इसका उपयोग कैसे करें? मुझे पूरा पथ, और सिर्फ फ़ाइल नाम दोनों की आवश्यकता है। – Specto

+0

प्रिंट os.path.basename (फ़ाइल) – anon58192932

+0

मैं यह कहना चाहूंगा कि यह ठीक से इंडेंट नहीं है। क्लास DirectoryWalker के बाद सब कुछ एक इंडेंट की जरूरत है। – anon58192932

उत्तर

6

'।' का उपयोग करने के बजाय। अपने निर्देशिका के रूप में, अपने पूर्ण पथ का संदर्भ लें:

for file in DirectoryWalker(os.path.abspath('.')): 
    print file 

इसके अलावा, मैं, यह अजगर भाषा में कुछ का मतलब है क्योंकि 'फ़ाइल' के अलावा किसी अन्य शब्द का उपयोग करते हुए सलाह देते हैं। कोई कीवर्ड नहीं, हालांकि यह अभी भी चलता है।

एक के रूप में एक तरफ, जब फ़ाइल नामों के साथ काम कर, मैं os.path मॉड्यूल अविश्वसनीय रूप से उपयोगी होने के लिए लगता है - मुझे लगता है कि के माध्यम से एक नज़र होने की सलाह देते हैं, विशेष रूप से

os.path.normpath 

को सामान्य पथ (अनावश्यक से छुटकारा मिलता है '। के और' theFolderYouWereJustIn /../ के)

os.path.join 

में शामिल दो रास्ते

+0

बहुत अच्छा काम किया। – Specto

+0

क्या कोई मेरी मदद कर सकता है कि फ़ाइल का उपयोग कैसे करें और इसका उपयोग कैसे करें? मुझे पूरा पथ, और सिर्फ फ़ाइल नाम दोनों की आवश्यकता है। – Specto

0

बस लौटे "./foo" पथ के लिए वर्तमान निर्देशिका पथ पहले जोड़ें:

print os.path.join(os.getcwd(), file) 
1

os.path.dirname()? os.path.normpath()? os.path.abspath()?

यह रिकर्सन सोचने के लिए एक सुंदर जगह भी होगी।

12

आप इतनी उबाऊ चीज़ क्यों करना चाहते हैं?

for path, directories, files in os.walk('.'): 
    print 'ls %r' % path 
    for directory in directories: 
     print ' d%r' % directory 
    for filename in files: 
     print ' -%r' % filename 

आउटपुट:

'.' 
    d'finction' 
    d'.hg' 
    -'setup.py' 
    -'.hgignore' 
'./finction' 
    -'finction' 
    -'cdg.pyc' 
    -'util.pyc' 
    -'cdg.py' 
    -'util.py' 
    -'__init__.pyc' 
    -'__init__.py' 
'./.hg' 
    d'store' 
    -'hgrc' 
    -'requires' 
    -'00changelog.i' 
    -'undo.branch' 
    -'dirstate' 
    -'undo.dirstate' 
    -'branch' 
'./.hg/store' 
    d'data' 
    -'undo' 
    -'00changelog.i' 
    -'00manifest.i' 
'./.hg/store/data' 
    d'finction' 
    -'.hgignore.i' 
    -'setup.py.i' 
'./.hg/store/data/finction' 
    -'util.py.i' 
    -'cdg.py.i' 
    -'finction.i' 
    -'____init____.py.i' 

लेकिन अगर आप जोर देते हैं, वहाँ os.path में है पथ से संबंधित उपकरण, os.basename तुम क्या देख रहे हैं।

>>> import os.path 
>>> os.path.basename('/hello/world.h') 
'world.h'