Python Forum
script closed before i can see the error that occurs?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
script closed before i can see the error that occurs?
#1
I am a newbie!!!!!!!!!!!! This code is pieces of other peoples code.
First a run down of what it does. Basically its a batch renamer.
  • Verifies iso file is a valid game from wiitdb database via wiitdb.txt
  • Copies iso files from current directory and subdirectories to "games" folder. In a 1:1 way. No compression.
  • Assigns game title and gameid to destination folder
  • Reads disc number, renaming iso 2 file to disc2, instead of game.iso
Now the issues. If it comes across an error it automatically closes before I can see what issue or iso caused the error. It ran fine through a few iso's. Than it hit a hiccup.
I assume a iso didn't match the list, but it closes to soon to see. Instead of continuing to the next iso it just stops. Also It has occasionally only half completed a couple. 

Any help would be appreciated.

import os
import re
import shutil
total_games = 0
dir = "./"
dirs = os.listdir("./")
wiitdb = open("wiitdb.txt", "r")
for root, dirs, files in os.walk(dir):
    if 'games' in dirs:
        dirs.remove('games')
    for file in files:
        if file.endswith(".iso"):
            pathfile = os.path.join(root, file)
            print("Reading file: "+ pathfile)
            f = open(pathfile)
            f.seek(0)
            header = f.read(7)
            gameid = header[:6]
            disc_number = ord(header[-1:])
            disc_number += 1
            print("GameID: "+ gameid + " Disc: "+ str(disc_number))
            match = re.search(r'\w+', gameid)
            if match:
                if not os.path.exists(dir + "games"):
                    os.makedirs(dir+"games")
                with open("wiitdb.txt", encoding='utf-8') as wiitdb:
                    for line in wiitdb:
                       m= re.match(gameid+r" = ([\w\W\s]+)", line)
                       if m:
                            gamename = m.group(1)
                            if ":" in gamename:
                                gamename = gamename.replace(':', ' - ')
                            if "\n" in gamename:
                                gamename = gamename.replace ('\n', '')
                            print("Game title: "+gamename)
                            newgamepath = dir+"games"+"/"+gamename+" ["+gameid+"]"
                            if not os.path.exists(newgamepath):
                                os.makedirs(newgamepath)
                                print("Copying game...")
                                shutil.copy(pathfile, newgamepath + "/game.iso")
                                total_games += 1
                            if  not os.path.exists(newgamepath+"/disc"+ str(disc_number)+".iso") and disc_number > 1:
                                print("Copying game...")
                                shutil.copy(pathfile, newgamepath + "/disc"+ str(disc_number) +".iso")
                                total_games += 1
            else:
                print("GameID: Invalid!!")
            print("====================================")
print("Total games copied: " + str(total_games))
Reply
#2
I think what you need to do is open a command line window, navigate to your script, and run it that way, instead of double-clicking it as I imagine you're currently doing. That should allow you to see the error message. If you're using an IDE, that should also allow you to run the code in a way where you could see the error message.
Reply
#3
I tried the command line. But got this error "script is not reconized as an internal or external command". I was thinking of maybe adding a counter to keep window open.

Edit. Tried again put used script.py instead of just script and it worked. Thank you for your help. Any one have suggestions on bypassing error and continuing on?

Here is my error:

 
Error:
Reading file: ./I-Ninja (USA).iso Traceback (most recent call last):   File "I:\wii\games\zzzz\script.py", line 19, in <module>     header = f.read(7)   File "C:\Users\Arcade\AppData\Local\Programs\Python\Python35-32\lib\encodings\ cp1252.py", line 23, in decode     return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 1062: cha racter maps to <undefined>
Reply
#4
Try opening the file in binary mode, that way you don't get decoding errors from python trying to understand what's in the file.
Reply
#5
Binary can work,but you have to get it over to a string before regex search.
Try set encoding.
f = open(pathfile, encoding="utf8")
Also set encoding in first open().
Reply
#6
When I said I am a noob. I mean it. This is my first attempt at python. I am trying my best to Google and learn as I go. But I have never been good at understanding, learning or writing any code. With that said im not looking for a hand out. I sort of understand most the code. But the error that is coming back. I do not understand what that line (19 and 23) does. Is there a program that translate code to English? lol.
Also with the binary and and encoding the first open you have me lost.

For the open I assume you are suggesting I state the file directory. The problem with this is I want to be able to drop the script and text into different folders without changing the code. If assumed wrong my apologies.

Please bear with the stupid and ignorant questions.

Also is there any free programs out there that could help me learn that are not text based. My comprehension of reading material for coding is awful.
Reply
#7
Let's start by thinking about files in general.

This script you wrote, is a plain text file. You can open it in notepad, and it'll look the same.
An iso file is not plain text. It contains many characters that you can't pronounce (...or draw), in order to compress itself to something small, instead of restricting itself to just what makes sense when you look at it.

Python tries to be very helpful and friendly. When you tell it to open a file, it tries to parse that file so it's easy to work with in your script. The error you're getting is because iso files contain nonsense characters, which python can't understand. The way around that, is to give python hints about what it should expect to see when it starts looking in that file.

For your code, that means we're talking about line 15: "f = open(pathfile)". If you change that line to what snippsat suggested, so it looks like this: "f = open(pathfile, encoding="utf8")", and change nothing else about your script at all, do you still get an error?
Reply
#8
(Dec-01-2016, 11:04 PM)wrestlemy69 Wrote:  I do not understand what that line (19 and 23) does. Is there a program that translate code to English? lol.
Python has interactive interpreter where you can test stuff out.
Here i just paste in line 19.
>>> gameid = header[:6]
Traceback (most recent call last):
 File "<string>", line 301, in runcode
 File "<interactive input>", line 1, in <module>
NameError: name 'header' is not defined

#Fix it
>>> header = 'Hello world'
>>> gameid = header[:6]
>>> gameid
'Hello '

>>> header[6:11]
'world'
[:6] is Slice notation sequence[start:stop:step]

Line 23:
>>> import re 

>>> gameid = 'hello world'
>>> match = re.search(r'\w+', gameid)
>>> match 
<_sre.SRE_Match object; span=(0, 5), match='hello'>
>>> bool(match)
True
>>> if match:
...     print('if True i run')
...     
if True i run
It's a good tool to use,and you can use it when you study the basic of Python.
Reply
#9
Thank you for the explanation. I tried that and it did not work but in a different way. There are a lot of iso in the folder. about 600. That error only come up with about 1 in 5 iso. When I run it, it skipps the ones already done normally. I run it. when it hits an error, I move that iso out of that folder and start again. This time with the changes, I left the iso in there that called issues. It did not skip the done iso this time, and went straight to an error. Different error.

Forgot to include error.

Error:
I:\wii\games\zzzz>script.py Reading file: ./Pokemon Box - Ruby & Sapphire (USA).iso Traceback (most recent call last):   File "I:\wii\games\zzzz\script.py", line 19, in <module>     header = f.read(7)   File "C:\Users\Arcade\AppData\Local\Programs\Python\Python35-32\lib\codecs.py" , line 321, in decode     (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc2 in position 28: invalid  continuation byte
Reply
#10
Read it is as binary and look at output of working files and compare with not working files.
import os
import re
import shutil

total_games = 0
dir = "./" # 1 file here to test
dirs = os.listdir("./")
wiitdb = open("wiitdb.txt", "r")
for root, dirs, files in os.walk(dir):
   if 'games' in dirs:
       dirs.remove('games')
   for file in files:
       if file.endswith(".iso"):
           pathfile = os.path.join(root, file)
           print("Reading file: "+ pathfile)
           f = open(pathfile, 'rb')
           f.seek(0)
           header = f.read(7)
           print(header) # Output to look at
       f.close()
It read the 7 first bytes of iso file.
So if a file has hello world in it output will be b'hello w'.

When i test with a iso file,i get this b'\x00\x00\x00\x00\x00\x00\x00'
So no header(only Null character) header can be in your iso files that work.

Just so know Python 3 makes a clear different between bytes and string(all strings are sequences of Unicode characters in Python 3).
You can convert over like this.
>>> s = b'hello w'
>>> type(s)
<class 'bytes'>
>>> s = s.decode('utf8')
>>> type(s)
<class 'str'>
>>> s
'hello w'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  OSError occurs in Linux. anna17 2 192 Mar-23-2024, 10:00 PM
Last Post: snippsat
  This result object does not return rows. It has been closed automatically dawid294 5 686 Jan-10-2024, 10:55 PM
Last Post: deanhystad
  A question about 'Event loop is closed' fc5igm 2 2,165 Oct-05-2021, 02:00 AM
Last Post: fc5igm
  ValueError: I/O operation on closed file problem aliwien 0 2,075 Apr-23-2021, 05:50 PM
Last Post: aliwien
  Run an app in a standalone terminal and wait until it's closed glestwid 2 2,447 Aug-30-2020, 08:14 AM
Last Post: glestwid
  how do i loop a "def ..." when error occurs? Stan2292 0 1,581 Aug-12-2019, 05:28 AM
Last Post: Stan2292
  Inclusive (closed) range with float numbers mapg 4 3,921 Apr-27-2019, 09:09 PM
Last Post: Gribouillis
  New df from existing when condition occurs Devilish 0 1,317 Jan-10-2019, 12:03 AM
Last Post: Devilish
  VMware View - Session opened/closed? hhh 0 2,297 May-31-2018, 10:10 AM
Last Post: hhh
  How to make Python stops for debugging when the error occurs? Tim 5 5,773 Feb-16-2018, 09:50 AM
Last Post: Tim

Forum Jump:

User Panel Messages

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