无法在python程序中打开下载目录

2024-05-17 13:35:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python程序,该程序会根据文件类型告诉我下载的文件数量,如4个音频文件、3个视频文件等。但我无法打开下载目录。是否有任何方法打开该目录,或者需要root/admin访问权限

这是密码

Scanner.py file:

import os
from core.GetPath import get_download_path

""" get downloads directory path (linux or windows) """
path = get_download_path()
path=path.replace('/','//')
path=path+"//"

audioFiles = []  
videoFiles = [] 
zipFiles = [] 
documentFiles = [] 
imageFiles = []  
otherFiles=[]    

print(path)

for file in os.listdir(path):
    try:
        if file.endswith(".mp3"):
            audioFiles.append(file)

        elif file.endswith(".docx"):
            documentFiles.append(file)

        elif file.endswith(".mp4"):
            videoFiles.append(file)

        elif file.endswith(".zip"):
            zipFiles.append(file)

        elif file.endswith(".jpge"):
            imageFiles.append(file)

        else:
            otherFiles.append(file)

    except Exception as e:
        raise e

获取\下载\路径取自此处: https://stackoverflow.com/a/48706260/11594438

输出:

//home//nandan//downloads//
Traceback (most recent call last):
  File "/media/nandan/G/Study/Learning/filedivider/Scanner.py", line 17, in <module>
    for file in os.listdir(path):
FileNotFoundError: [Errno 2] No such file or directory: '//home//nandan//downloads//'

Process finished with exit code 1


Tags: pathinpyimport程序目录getos