Python Forum
Trying to convert a script from "2to3"
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to convert a script from "2to3"
#11
so the picture show that script is executed via Python2... so it does not work even in python2?
Reply
#12
(Feb-21-2017, 05:40 PM)buran Wrote: so the picture show that script is executed via Python2... so it does not work even in python2?

It works fine in Python2. Python3 says that the files are all not found even when they are in the same folder and works when using Python2. Sorry for the late response.
Reply
#13
The picture from the previous post is from executing the script with python2 - look at the title bar of the window on the picture... So it either doesn't work in python2 or if you think it's from executing the script with python3 you are not right.
Reply
#14
Okey so I'm in the same position as JonathanMonkey right now. Trying to convert the exact same script to Python 3.6 from 2.7

At first I got an error on line 49. After some searching I found out that I should add a second slash instead of just one.
Quote:Traceback (most recent call last):
File "MetaVerifiy.py", line 49, in <module>
f.seek(actDimensions[0]*actDimensions[1]*(actDimensions[2]/8),1)
TypeError: 'float' object cannot be interpreted as an integer

Now, after line 49 seems to be fixed line 50 is now throwing an error. Does anyone know what can be done to fix this? I've tried searching for an answer myself but I'm very new to Python.
Quote:Traceback (most recent call last):
File "MetaVerifiy.py", line 50, in <module>
f.write("\x00\x00\x00\x00\x00\x00\x00\x00TRUEVISION-XFILE\x2E\x00")
TypeError: a bytes-like object is required, not 'str'

Full code:
import struct
import msvcrt as m
import os

def wait():
    m.getch()
    
def readByte(file):
    return struct.unpack("B", file.read(1))[0]
 
def readu16le(file):
    return struct.unpack("<H", file.read(2))[0]
 
def readu32le(file):
    return struct.unpack("<I", file.read(4))[0]


tgas = ["iconTex.tga","bootLogoTex.tga","bootDrcTex.tga","bootTvTex.tga"]
dimensions = [[128,128,32],[170,42,32],[854,480,24],[1280,720,24]]
for i in range(len(tgas)):
    tga = tgas[i]
    dimension = dimensions[i]
    if os.path.exists(tga):
        with open(tga,"rb+") as f:
            header = readu32le(f)
            if header != 0x00020000:
                print(tga + "is compressed. it cant be compressed!")
                break
            f.seek(12)
            actDimensions = [readu16le(f),readu16le(f),readByte(f)]
            hasHadBadDiment = False
            for j in range(len(actDimensions)):
                if j == 0:
                    type = "width"
                elif j == 1:
                    type = "height"
                else:
                    type = "depth"
                diment = dimension[j]
                actDiment = actDimensions[j]
                if diment != actDiment:
                    if not hasHadBadDiment:
                        hasHadBadDiment = True
                        print("dimensions are not valid for: " + tga)
                    print(type + " is: " + str(actDiment) + " should be: " + str(diment))
            if hasHadBadDiment:
                break
            f.seek(1,1)
            f.seek(actDimensions[0]*actDimensions[1]*(actDimensions[2]//8),1)
            f.write("\x00\x00\x00\x00\x00\x00\x00\x00TRUEVISION-XFILE\x2E\x00")
    else:
        print(tga + " could not be found!")
print("All TGA's verified!")
print("press any key to exit...")
#todo verifiy bootovie.h264
wait()        
        
Reply
#15
Stick a b in front of it.
>>> "\x00"
'\x00'
>>> b"\x00"
b'\x00'
>>> type("\x00")
<class 'str'>
>>> type(b"\x00")
<class 'bytes'>
Reply
#16
It worked! Thanks a lot!

Full working piece of code in case anyone else would need it in the future.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Script to convert Json to CSV file chvsnarayana 8 2,498 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,640 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Convert .py script to dll file and .exe srikanthpython 0 3,127 Aug-20-2020, 06:01 PM
Last Post: srikanthpython
  Convert Python script to Php JuroClash 0 8,145 Oct-21-2019, 03:01 PM
Last Post: JuroClash
  convert sh script to python script Reims 0 2,143 Oct-01-2019, 12:10 PM
Last Post: Reims
  Is there any way to convert a python script (with Tkinter module), to a .exe (Windows moste 3 4,001 May-12-2019, 12:02 PM
Last Post: snippsat
  How to use 2to3.py converter under Windows OS samsonite 2 7,300 Mar-02-2019, 05:49 AM
Last Post: samsonite
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,826 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Issue with a script to convert xls to json Will86 2 3,808 Dec-19-2018, 08:23 AM
Last Post: Will86
  Error with using pyinstaller to convert python script in Mac OS Takeshio 2 5,364 Oct-19-2018, 02:42 PM
Last Post: Takeshio

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020