简体   繁体   English

如何测试字符串是否引用文件或目录? 用正则表达式? 在蟒蛇?

[英]How can I test if a string refers to a file or directory? with regular expressions? in python?

so I'm writting a generic backup application with os module and pickle and far I've tried the code below to see if something is a file or directory (based on its string input and not its physical contents).所以我正在编写一个带有os模块和pickle的通用备份应用程序,到目前为止我已经尝试了下面的代码来查看某个东西是文件还是目录(基于它的字符串输入而不是它的物理内容)。

import os, re

def test(path):
    prog = re.compile("^[-\w,\s]+.[A-Za-z]{3}$")
    result = prog.match(path)
    if os.path.isfile(path) or result:
        print "is file"
    elif os.path.isdir(path):
        print "is directory"
    else: print "I dont know"

Problems问题

test("C:/treeOfFunFiles/")
is directory
test("/beach.jpg")
I dont know
test("beach.jpg")
I dont know
test("/directory/")
I dont know

Desired Output期望的输出

test("C:/treeOfFunFiles/")
is directory
test("/beach.jpg")
is file
test("beach.jpg")
is file
test("/directory/")
is directory

Resources资源

what regular expression should I be using to tell the difference between what might be a file and what might be a directory ?我应该使用什么正则表达式来区分什么是file ,什么是directory or is there a different way to go about this?还是有不同的方法来解决这个问题?

The os module provides methods to check whether or not a path is a file or a directory. os模块提供了检查路径是文件还是目录的方法。 It is advisable to use this module over regular expressions.建议在正则表达式上使用此模块。

>>> import os
>>> print os.path.isfile(r'/Users')
False
>>> print os.path.isdir(r'/Users')
True

This might help someone, I had the exact same need and I used the following regular expression to test whether an input string is a directory, file or neither: for generic file:这可能对某人有帮助,我有完全相同的需求,我使用以下正则表达式来测试输入字符串是目录、文件还是两者都不是:对于通用文件:

^(\/+\w{0,}){0,}\.\w{1,}$

for generic directory:对于通用目录:

^(\/+\w{0,}){0,}$

So the generated python function looks like:所以生成的 python 函数如下所示:

import os, re

def check_input(path):
    check_file = re.compile("^(\/+\w{0,}){0,}\.\w{1,}$")
    check_directory = re.compile("^(\/+\w{0,}){0,}$")
    if check_file.match(path):
        print("It is a file.")
    elif check_directory.match(path):
        print("It is a directory")
    else:
        print("It is neither")

Example:例子:

  • check_input("/foo/bar/file.xyz") prints -> Is a file check_input("/foo/bar/file.xyz") 打印 -> 是一个文件
  • check_input("/foo/bar/directory") prints -> Is a directory check_input("/foo/bar/directory") 打印 -> 是一个目录
  • check_input("Random gibberish") prints -> It is neither check_input("Random gibberish") 打印 -> 两者都不是

This layer of security of input may be reinforced later by the os.path.isfile() and os.path.isdir() built-in functions as Mr.Squig kindly showed but I'd bet this preliminary test may save you a few microseconds and boost your script performance.这层输入的安全性稍后可能会通过 os.path.isfile() 和 os.path.isdir() 内置函数得到加强,正如 Squig 先生亲切地展示的那样,但我敢打赌这个初步测试可以为您节省一些微秒并提高脚本性能。

PS: While using this piece of code, I noticed I missed a huge use case when the path actually contains special chars like the dash "-" which is widely used. PS:在使用这段代码时,我注意到我错过了一个巨大的用例,因为路径实际上包含特殊字符,例如广泛使用的破折号“-”。 To solve this I changed the \w{0,} which specifies the requirement of alphabetic only words with.{0,} which is just a random character.为了解决这个问题,我更改了 \w{0,} 指定仅字母单词的要求。{0,} 这只是一个随机字符。 This is more of a workaround than a solution.这更像是一种解决方法而不是解决方案。 But that's all I have for now.但这就是我现在所拥有的。

In a character class, if present and meant as a hyphen, the - needs to either be the first/last character, or escaped \- so change "^[\w-,\s]+\.[A-Za-z]{3}$" to "^[-\w,\s]+\.[A-Za-z]{3}$" for instance.在字符类中,如果存在并表示为连字符,则-需要是第一个/最后一个字符,或者转义\-因此更改"^[\w-,\s]+\.[A-Za-z]{3}$""^[-\w,\s]+\.[A-Za-z]{3}$"例如。

Otherwise, I think using regex's to determine if something looks like a filename/directory is pointless...否则,我认为使用正则表达式来确定某些东西是否看起来像文件名/目录是毫无意义的......

  • /dev/fd0 isn't a file or directory for instance /dev/fd0不是文件或目录
  • ~/comm.pipe could look like a file but is a named pipe ~/comm.pipe可能看起来像一个文件,但实际上是一个命名管道
  • ~/images/test is a symbolic link to a file called '~/images/holiday/photo1.jpg' ~/images/test是指向名为“~/images/holiday/photo1.jpg”的文件的符号链接

Have a look at the os.path module which have functions that ask the OS what something is...:查看os.path模块,它具有询问操作系统什么是什么的功能......:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM