python - Get file name only (without extension and directory) from file path -


मेरे पास क्या है:

  import os import ntpath def fetch_name (फ़ाइलपथ): वापस आओ .path.splitext (ntpath.basename (filepath)) [0] a = u'E: \ यह यहाँ पर कुछ स्ट्रिंग है \ news_20.03_07.30_10 .m2t 'b = u'E: \ और यहाँ कुछ स्ट्रिंग भी है \ Logo_TimeBuffer.m2t.mpg 'fetch_name (a) & gt; & gt; यू; वह यहाँ पर कुछ स्ट्रिंग है news_20.03_07.30_10' # गलत! Fetch_name (b) & gt; & gt; u'Logo_TimeBuffer.m2t '# गलत!   

मुझे क्या चाहिए:

  fetch_name (a)> gt; & gt; u'news_20.03_07.30_10 'fetch_name (बी) & gt; & gt; u 'लोगो_टाइम बफर'    

आपका कोड बिल्कुल वैसे ही काम करता है जितना चाहिए उदाहरण के लिए, \ n को बैकस्लैश वर्ण के रूप में नहीं माना जाता है क्योंकि यह एक नया अक्षर है उदाहरण b में, एक्सटेंशन है। एमपीजी, जो ठीक से हटा दिया गया है। एक फ़ाइल में कभी भी एक से अधिक एक्सटेंशन नहीं हो सकता है, या किसी अवधि में कोई एक्सटेंशन हो सकता है।

पहली अवधि से पहले ही थोड़ा सा प्राप्त करने के लिए, आप ntpath.basename (filepath) .split (' । ') [0] , लेकिन संभवत: यह नहीं है कि आप क्या चाहते हैं क्योंकि फ़ाइल नामों की अवधि पूरी करने के लिए यह बिल्कुल कानूनी है।

Comments

Popular posts from this blog

c++ - Cmake produces file extensions in static library archives -

c# - Roxy file manager in MVC doesn't accept session path -

c# - XML - Serialize class - Some questions -