Skip to content

TypeError: a bytes-like object is required, not 'str' #29

Closed
@CyberDefend3r

Description

@CyberDefend3r

I am trying to do something super simple. Well that I thought was simple. I spent like 20 min writing the code and about 2 hours trying to figure out why it doesn't work...

I keep getting a typeError from subprocess called from the exiftool start(). I am sure I am doing something wrong but I cant figure it out. I thought i would post here on the very slim chance this is a bug and not my fault.

"""
Used to take pictures seperated into folders based on date like year/month/photo.jpg, and 
change the create date of photo to the year and month from the folder names.

The script assumes that exiftool.exe and script are in the same directory. year folders are 
in same dir as well.
"""

import os
from glob import glob
import exiftool

class collection:
    def __init__(self):
        self.dateFile = []
        self.fileList = {}
        self.paths = glob('**/**')
        self.curDir = os.getcwd()
        self.exifTool = os.path.join(self.curDir.encode(), 'exiftool.exe'.encode())

    def setVariables(self):
        for path in self.paths:
            globPath = os.path.join(path, '*.*')
            picturPath = glob(globPath)
            pictures = []
            for pic in picturPath:
                pictures.append(os.path.join(self.curDir, pic))
            date = str.replace(path, '\\', ':') + ':01 00:01:01'
            self.fileList = {'date':date, 'pictures':pictures}
            self.dateFile.append(self.fileList)

class changeDate:
    def __init__(self, collection):
        self.dateFile = collection.dateFile
        self.exifTool = exiftool.fsencode(collection.exifTool)

    def buildEXIFcommand(self):
        for filelist in self.dateFile:
            date = filelist['date']
            for picfile in filelist['pictures']:
                modDate = '"' + '-FileCreateDate=' + date + '"'
                modDate.encode()
                picfile = exiftool.fsencode(picfile)
                self.exifChange(modDate, picfile)

    def exifChange(self, modDate, picfile):
        with exiftool.ExifTool(self.exifTool) as et:
            et.execute(modDate, picfile)

if __name__ == "__main__":
    Collection = collection()
    Collection.setVariables()
    ChangeDate = changeDate(Collection)
    ChangeDate.buildEXIFcommand()

Traceback (most recent call last):
  File "c:\Users\trevo\.vscode\extensions\ms-python.python-2019.10.44104\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    main(ptvsdArgs)
  File "c:\Users\trevo\.vscode\extensions\ms-python.python-2019.10.44104\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 432, in main
    run()
  File "c:\Users\trevo\.vscode\extensions\ms-python.python-2019.10.44104\pythonFiles\lib\python\old_ptvsd\ptvsd\__main__.py", line 316, in run_file
    runpy.run_path(target, run_name='__main__')
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "c:\Users\trevo\OneDrive\Desktop\pics\datchange.py", line 46, in <module>
    ChangeDate.buildEXIFcommand()
  File "c:\Users\trevo\OneDrive\Desktop\pics\datchange.py", line 36, in buildEXIFcommand
    self.exifChange(modDate, picfile)
  File "c:\Users\trevo\OneDrive\Desktop\pics\datchange.py", line 39, in exifChange
    with exiftool.ExifTool(self.exifTool) as et:
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exiftool.py", line 192, in __enter__
    self.start()
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exiftool.py", line 175, in start
    stderr=devnull)
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1119, in _execute_child
    args = list2cmdline(args)
  File "C:\Users\trevo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 530, in list2cmdline
    needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: a bytes-like object is required, not 'str'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions