ハッシュが利用可能な場合 (ハッシュとファイル パスでいっぱいの csv に対してチェックし、データ フレームに列を追加する)、ファイルを新しい場所に移動する Python スクリプトがあります。
パスが文字列である必要があるときに、パスがフロートとして出力されるスクリプトで問題が発生しています。私の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