Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将路径列表传递给另一个函数_Python_Python 3.x - Fatal编程技术网

Python 将路径列表传递给另一个函数

Python 将路径列表传递给另一个函数,python,python-3.x,Python,Python 3.x,因此,这段代码最初是一个函数,在帮助下,我将其分解为两个单独的函数,但我仍在努力使其正常工作。如有任何指导,将不胜感激 # ------------------------------------------- # Checks to see if listed folders exists and then deletes # ------------------------------------------- def check_directory(path): # retur

因此,这段代码最初是一个函数,在帮助下,我将其分解为两个单独的函数,但我仍在努力使其正常工作。如有任何指导,将不胜感激

# -------------------------------------------
# Checks to see if listed folders exists and then deletes
# -------------------------------------------


def check_directory(path):
    # returns true if path is an existing directory
    return os.path.exists(path) and os.path.isdir(path)


dirs_to_delete = [
    'C:\Folder Here',
    'C:\Folder Here1',
    'C:\Folder Here2',
    'C:\Folder Here3'

 ]


def remove_directory(pathlist):
    for path in pathlist:
        if check_directory(path):
            shutil.rmtree(path)
            print(colored('Found ' + path + ' removing', 'green'))
我正在调用这个函数

remove_directory()     #Checks and Removes directories listed above
我在运行时遇到以下错误

remove_directory()  #Checks and Removes directories listed above
TypeError: remove_directory() missing 1 required positional argument: 'pathlist'

在您的帖子中,您似乎正在调用
remove\u directory
,但没有按需要传递列表,

将调用更改为
remove_directory()
remove_directory(dirs_to_delete)
您需要将
dirs_to_delete
传递到以下函数:


remove\u目录(dirs\u to\u delete)
调试

TypeError: remove_directory() missing 1 required positional argument: 
def check_directory(path):
    # returns true if path is an existing directory
    return os.path.exists(path) and os.path.isdir(path)


dirs_to_delete = [
    'C:\Folder Here',
    'C:\Folder Here1',
    'C:\Folder Here2',
    'C:\Folder Here3'

 ]

def remove_directory(pathlist):
    for path in pathlist:
        if check_directory(path):
            shutil.rmtree(path)
            print(colored('Found ' + path + ' removing', 'green'))

remove_directory(dirs_to_delete)   # pass the list to the function here
因为

TypeError: remove_directory() missing 1 required positional argument: 
def check_directory(path):
    # returns true if path is an existing directory
    return os.path.exists(path) and os.path.isdir(path)


dirs_to_delete = [
    'C:\Folder Here',
    'C:\Folder Here1',
    'C:\Folder Here2',
    'C:\Folder Here3'

 ]

def remove_directory(pathlist):
    for path in pathlist:
        if check_directory(path):
            shutil.rmtree(path)
            print(colored('Found ' + path + ' removing', 'green'))

remove_directory(dirs_to_delete)   # pass the list to the function here
函数
remove\u directory()
需要一个参数,在这种情况下可能是
dirs\u to\u delete

因此

TypeError: remove_directory() missing 1 required positional argument: 
def check_directory(path):
    # returns true if path is an existing directory
    return os.path.exists(path) and os.path.isdir(path)


dirs_to_delete = [
    'C:\Folder Here',
    'C:\Folder Here1',
    'C:\Folder Here2',
    'C:\Folder Here3'

 ]

def remove_directory(pathlist):
    for path in pathlist:
        if check_directory(path):
            shutil.rmtree(path)
            print(colored('Found ' + path + ' removing', 'green'))

remove_directory(dirs_to_delete)   # pass the list to the function here

您已经定义了
remove_directory
以获取单个位置参数,
pathlist
,但是,它不获取全局范围内变量
pathlist
的值:

s=100
def打印值:
印刷品
#我没有给它一个位置参数,所以没有定义s
打印值()
#引发TypeError,因为函数内的作用域
#不知道s是什么
#现在我给它这个值
打印值(s)
# 100
所以对于你的问题,你需要通过这个参数

#这就是在脚本中调用函数的方式
删除目录(路径列表)

remove\u directory函数需要参数
pathlist

由于未为
路径列表添加默认值,因此调用
删除目录()
失败

尝试:

def remove_目录(路径列表=[]):
对于路径列表中的路径:
如果检查目录(路径):
shutil.rmtree(路径)
打印(彩色('Found'+path+'removing','green'))
或在调用函数时提供一个列表:

删除目录([])

您需要将
dirs\u传递给\u delete
传递给
remove\u directory()
您需要将一个参数传递给函数
remove\u directory()
Try:
remove\u dirs\u to\u delete)