0

我有一个 Python 脚本,如果哈希可用(检查充满哈希和文件路径的 csv,然后将列添加到数据框中),那么我想将文件移动到新位置。

我在我的脚本中遇到了一个问题,当它应该是一个字符串时,我的路径以浮点数的形式出现。在我的 jupyter 笔记本中,当我执行print(type(path))输出时,<class 'str'>但是当我print(type(path))在命令行中运行脚本时,它返回<class 'float'>.

当我运行完整的脚本时,我收到以下错误:

错误

Traceback (most recent call last):
  File "compare_hashes_to_image_hashes.py", line 82, in <module>
    for source_filename in os.listdir(path):
TypeError: listdir: path should be string, bytes, os.PathLike, integer or None, not float

为什么它在我的 jupyter 笔记本中有效,但在我的脚本中无效?

脚本(与问题相关的部分)

#!/usr/bin/python36

import os
import shutil

# make columns into lists
users = list(master_df['USER_JID'])
avails = list(master_df['HASH_AVAILABLE'])
paths = list(df_merged['FILE_PATH'])
hashes = list(df_merged['SHA1_BASE16'])

# define source location and destination location
destination = "/opt/PhotoTips/input/"
source_img = "/opt/PhotoTips/hash_images/"

"""
Move images to appropriate user img folders
"""

# for every value in available list
for i in range(len(avails)):
    # each value in user list is a user
    user = users[i]
    # each value in path list is a path
    path = paths[i]
    # each value in hash value list is a hash
    hash_val = hashes[i]
    # build full location
    location = destination + user + '/img'
    # if hash image is available
    if 'Available' in avails[i]:
        # debug purposes
        # print(type(path)) # returns <class 'float'>
        # suggested by marklap
        # if isinstance(path, float): 
            # print(path)
            # returns nan
        for source_filename in os.listdir(path):   <---- line with the issue
            # confirm text file for match
            if source_filename.startswith(hash_val):
                # build full path
                source_file_path = os.path.join(path, source_filename)
                # copy image to appropriate user location
                shutil.copy(source_file_path, location)
    # if hash image is NOT available
    else:
        # do nothing
        pass
4

0 回答 0