Search code examples
pythonloopsfor-loopiterationskip

Python: Skip 'for' loop interation


I couldn't find a question that matches this specific issue; I'm writing an automatic code-minifier in Python, but I can't seem to find out how to skip the current iteration of the 'for' loop.

Can you help?

Here's the script:

import os
import sys
try:
    file_path = sys.argv[1]
except IndexError:
    print("No file given")
    sys.exit()

minified_file = ""
dbl_quotes = False
sgl_quotes = False
line_comment = False
multi_line_comment = False
current_index = 0

if os.path.isfile(file_path):
    print('Path: ' + file_path)
    char_check = ""
    file_handle = open(file_path)
    file_content = file_handle.read()
    for file_char in file_content:
        if sgl_quotes or dbl_quotes:
            if file_char == "'" and not dbl_quotes:
                sgl_quotes = False
            if file_char == '"' and not sgl_quotes:
                dbl_quotes = False
            minified_file += file_char
            continue
        else:
            if file_char == "'":
                sgl_quotes = True
                minified_file += file_char
                continue
            elif file_char == '"':
                dbl_quotes = True
                minified_file += file_char
                continue

            if current_index+1 < len(file_content):
                if file_char == '/' and file_content[current_index+1] == '*':
                    multi_line_comment = True

    if current_index-1 >= 0:
        if multi_line_comment == True and file_char == '/' and file_content[current_index-1] == '*':
            multi_line_comment = False
            continue

    if current_index+1 < len(file_content):
        if file_char == '/' and file_content[current_index+1] == '/' and not line_comment:
            line_comment = True
            continue

    if line_comment:
        if file_char == '\r' or file_char == '\n':
            line_comment = False
            continue

    if line_comment or multi_line_comment:
        continue

    if file_char != '\r' and file_char != '\n' and file_char != '\t':
        minified_file += file_char

    current_index += 1

print(minified_file)
wait = input("PRESS ENTER TO CONTINUE.")

Solution

  • I have found the issue:

    The snippet:

    if current_index+1 < len(file_content):
        if file_char == '/' and file_content[current_index+1] == '*':
            multi_line_comment = True
    

    Was tabulated one tab too far forward.

    I'm really not looking forward to learning the rest of Python.