-1

i have windows 11 python 3.9, im runing a py script to make a launcher, i resolved a problem with long renaming to int but now i cant determine whats going on, the error is :

f.write(buf)

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

import os
import zlib

_np = os.path.normpath
_j = os.path.join
def make_path(a, b):
    return _np(_j(a, b)).replace("\\", "/")

def get_crc32(filename):
    crc = 0
    with open(filename, "rb") as f:
        crc = zlib.crc32(f.read())
    return "%x" % (crc & 0xffFFffFF)

def get_mtime(filename):
    # http://support.microsoft.com/kb/167296
    # How To Convert a UNIX time_t to a Win32 FILETIME or SYSTEMTIME
    EPOCH_AS_FILETIME = 116444736000000000 # January 1, 1970 as MS file time
    HUNDREDS_OF_NANOSECONDS = 10000000
    return EPOCH_AS_FILETIME + int(os.path.getmtime(filename)) * HUNDREDS_OF_NANOSECONDS

input_folder = _np("client")
output_folder = _np("web")
output_version = _np("0.0.0.2")
output_fv = make_path(output_folder, output_version)
output_crclist = make_path(output_fv, "crclist")

file_list = []
for root, dir, files in os.walk(input_folder):
    for filename in files:
        file_elem = {}
        file_elem["path"] = make_path(root, filename)
        file_elem["real_path"] = file_elem["path"][len(input_folder)+1:].replace("/", "\\")
        # crc32 calculation
        file_elem["crc32"] = get_crc32(file_elem["path"])
        # size calculation
        file_elem["size"] = os.path.getsize(file_elem["path"])
        # mtime calculation
        mtime = get_mtime(file_elem["path"])
        file_elem["mtime1"] = mtime >> 32
        file_elem["mtime2"] = mtime & 0xFFffFFff
        # add in list
        file_list.append(file_elem)

# make path if missing
if not os.path.exists(output_fv):
    os.makedirs(output_fv)

# generate crclist
print("Generating crclist:")
with open(output_crclist, "wb") as f:
    for elem in file_list:
        buf = "%s %d %d %d %s\n" % (elem["crc32"], elem["size"], elem["mtime1"], elem["mtime2"], elem["real_path"])
        f.write(buf)
        print(buf.replace("\n", ""))

# generate lz files
ENABLE_LZ_GENERATION = True
if ENABLE_LZ_GENERATION:
    import subprocess
    print("Generating .lz:")
    for elem in file_list:
        filepath_in = make_path(input_folder, elem["real_path"])
        filepath_out = make_path(output_fv, elem["real_path"]) + ".lz"
        # create out dir
        dirpath_out = os.path.dirname(filepath_out)
        if not os.path.exists(dirpath_out):
            os.makedirs(dirpath_out)
        # create lz
        cmd = 'mt2lz pack "%s" "%s"' % (filepath_in, filepath_out)
        subprocess.call(cmd, shell=True)
        print(cmd)
#
Raul6969
  • 13
  • 8
  • 1
    Is your goal to write bytes to the file? You could just write strings, replace "wb" with "w" (or "w+") otherwise you need to convert your str to bytes (https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – Schalton Oct 04 '21 at 15:49
  • you are right that solved – Raul6969 Oct 04 '21 at 15:53

1 Answers1

0

It looks like your goal is to write strings:

with open(output_crclist, "w+") as f: # "w" or "w+" to create the file if it doesn't exist
    for elem in file_list:
        output_string = "%s %d %d %d %s\n" % (elem["crc32"], elem["size"], elem["mtime1"], elem["mtime2"], elem["real_path"])
        f.write(output_string )
        print(output_string .replace("\n", ""))

If your goal is to write bytes, you'll need to convert the string before wrtiting it: Best way to convert string to bytes in Python 3?

with open(output_crclist, "wb") as f:
    for elem in file_list:
        string = "%s %d %d %d %s\n" % (elem["crc32"], elem["size"], elem["mtime1"], elem["mtime2"], elem["real_path"])
        buf = string.encode()
        f.write(buf)
        print(buf.replace("\n", ""))
Schalton
  • 2,867
  • 2
  • 32
  • 44