Python Search module with regular

Source: Internet
Author: User
Tags glob

Glob is a module that Python provides to find files that support regular expressions.

Os.listdir () and Fnmatch.fnmatch () are used in the implementation. But there's no real invoking a subshell.

Glob.glob (pathname)    Return A possiblystring containing a path specification. Pathname can is either absolute (like/usr/src/python-1.5/makefile) or relative (like: /.. /tools/*/*.gif), and can contain Shell-style wildcards. Broken Symlinks is included in the results (as in the shell).

The key is to support the symlinks.

The Glob module contains two main methods:

Glob.glob

This method returns a list of all matching file paths that require a parameter to specify a matching path string (This string can be either an absolute path or a relative path), and its returned file name includes only the filename in the current directory, not the files in the subfolder.

Import Glob     # get all pictures  under the specified directory Print Glob.glob (r"e:\picture\*\*.jpg")    #  Get all. py files  for the parent directory print glob.glob (r'). /*.py'# relative path  

Glob.iglob

Gets a programmable calendar object that can be used to obtain a matching file path name one at a. The difference from Glob.glob () is that Glob.glob gets all the matching paths at the same time, and Glob.iglob only gets one matching path at a time

Import Glob     # . py files  in the parent directory F = Glob.iglob (R'). /*.py')    print#<generator object Iglob at 0x00b9ff80>     for inch F:       Print py  

symlinks, soft links (Symbolic links). Create a link in the Windows7 system:

Command format: mklink/d (definition parameter) \mydocs (link file) \users\user1\documents (original file)

Detailed parameters:

/D: Create a symbolic link for the directory link (symbolic Link)/J: Create a directory with a soft link (join) (junction)/h: Hard link to create a file

Create a link to a file

Mklink Targrtfile Srcfile

Create a link to a folder

MKLINK/D TargetFolder Srcfolder

Deleting the virtual link directory does not delete the remote folder real files, note that you must not use Del,del to delete the remote real files.

Attention:

1. When the dir link is located in the directory, you will see this link, and the specific path information will be given. The icon and shortcut for the symbolic link are similar, but the shortcut's extension is. Ink, and the file type of the symbolic link is the same as its destination file.

2. Since the symbolic link itself is just a link, modifying the symbolic link will affect the target file and vice versa. If you modify the properties of a symbolic link, such as setting its properties to hidden, it affects the destination file at the same time.

3. If you delete the target file and access its symbolic link, Windows will always try to find the target file, so it will be an error to find the file. If you delete a symbolic link, there is no effect on the destination file.

Python Search module with regular

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.