Ways to view file names and file paths in Python

Source: Internet
Author: User
viewing file names and files paths

>>> import os>>> url = ' http://images.cnitblog.com/i/311516/201403/020013141657112.png ' >> > filename = os.path.basename (URL) >>> filepath = os.path.dirname (URL) >>> filename ' 020013141657112.png ' >>> filepath ' http://images.cnitblog.com/i/311516/201403 ' >>>


(Os.path.realpath ())    (Os.path.dirname (Os.path.realpath ()))  (Os.path.basename (Os.path.realpath ()))


Print (Os.listdir (dirname))     # displays only the file name and directory name under that directory, does not contain files in subdirectories, and defaults to the directory where the current file resides
import os# os.walk () traverse all Files under Folder # Os.walk () get three sets of data (RootDir, Dirname,filnames ) def file_path (File_dir): For root, dirs, files in Os.walk (File_dir): print (Root, end= ") # current directory path PR Int (dirs, end= ") # All subdirectories under the current path print (files) # All non-directory sub-files under the current directory 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.