python列出指定目录下指定字符的文件

def list_all_files(rootdir, pattern="", is_recurse=False):
    import os
    _files = []
    pattern=pattern if pattern=="" else pattern.split("&")
    words_contained=[x for x in pattern if x.find("~")==-1]
    words_not_contained=[w[1:] for w in pattern if w.find("~")!=-1]

    #列出文件夹下所有的目录与文件
    list_file = os.listdir(rootdir)
    
    for i in range(0,len(list_file)):
        # 构造路径
        path = os.path.join(rootdir,list_file[i])

        # 判断路径是否是一个文件目录或者文件
        # 如果是文件目录,继续递归        
        if is_recurse==True and os.path.isdir(path):
            _files.extend(list_all_files(path))
        if os.path.isfile(path):
            if pattern=="":
                _files.append(path)
            else:                
                is_contained=True if len([1 for w in words_contained if path.find(w)!=-1])==len(words_contained) else False
                is_not_contained=True if len([1 for w in words_not_contained if path.find(w)!=-1])!=0 else False
                if is_contained==True and is_not_contained==False:
                    _files.append(path)                
    return _files
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在 Python ,你可以使用 `os` 模块来指定文件目录。下面是一些常用的方法: 1. 获取当前工作目录: ```python import os print(os.getcwd()) ``` 2. 更改当前工作目录: ```python import os os.chdir('/path/to/new/dir') ``` 3. 获取目录下的文件列表: ```python import os file_list = os.listdir('/path/to/dir') print(file_list) ``` 4. 检查文件目录是否存在: ```python import os if os.path.exists('/path/to/file_or_dir'): print('File or directory exists') else: print('File or directory does not exist') ``` 5. 创建目录: ```python import os os.mkdir('/path/to/new/dir') ``` 6. 创建多级目录: ```python import os os.makedirs('/path/to/new/dir/multiple/levels') ``` 请注意,这些示例仅介绍了一些常用的方法,`os` 模块还有很多其他功能,你可以查看 Python 官方文档来获取更多信息。 ### 回答2: 在Python,我们可以使用`os`模块来指定文件目录。首先,我们需要导入`os`模块。然后,可以使用`os.chdir()`函数来改变当前工作目录。 `os.chdir()`函数接受一个字符串参数,该参数是你想要指定目录路径。例如,如果想要将当前工作目录更改为`/home/user/Documents`,可以使用以下代码: ```python import os os.chdir("/home/user/Documents") ``` 这样,当前工作目录就变成了`/home/user/Documents`。接下来的文件操作将在新指定目录下进行。 你也可以使用`os.getcwd()`函数来获取当前工作目录。例如,你想要获取当前工作目录并打印出来,可以使用以下代码: ```python import os current_dir = os.getcwd() print("当前工作目录:", current_dir) ``` 输出将为当前工作目录的路径。 除了改变当前工作目录,我们还可以使用`os.path`模块来指定文件目录。`os.path`模块提供了一组用于处理文件路径的函数。其,`os.path.join()`函数可以用于拼接路径。 例如,如果想要拼接目录`/home/user`和文件名`file.txt`,可以使用以下代码: ```python import os path = os.path.join("/home/user", "file.txt") print("拼接后的路径:", path) ``` 输出将为`/home/user/file.txt`。 以上就是使用Python指定文件目录的方法。我们可以使用`os.chdir()`函数改变当前工作目录,或使用`os.path.join()`函数拼接路径。使用这些函数,我们可以更方便地操作文件目录。 ### 回答3: 在Python,可以使用`os`模块来指定文件目录。 首先,需要导入`os`模块: ```python import os ``` 然后,可以使用`os.chdir()`方法来指定文件目录。该方法接受一个字符串参数,表示要切换到的目录路径。 例如,假设要切换到名为`mydir`的目录: ```python os.chdir('mydir') ``` 如果`mydir`目录位于当前工作目录之外,需要提供绝对路径或者相对于当前工作目录的相对路径。 在切换完成后,当前工作目录将变为指定目录。 通过`os.getcwd()`方法可以获取当前工作目录的路径。 ```python current_dir = os.getcwd() print(current_dir) ``` 此外,还可以使用`os.path.join()`方法来创建指定目录下的文件路径。该方法接受多个参数,会自动根据操作系统的不同,选择正确的路径分隔符。 例如,如果要指定`mydir`目录下的`myfile.txt`文件路径: ```python file_path = os.path.join('mydir', 'myfile.txt') print(file_path) ``` 最后,还可以使用`os.listdir()`方法来列出指定目录下的文件文件夹。 ```python files = os.listdir('mydir') print(files) ``` 以上就是在Python指定文件目录的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坚果仙人

谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值