randomperson
randomperson

Reputation: 11

Why does my code bring an error up randomly?

Hey I'm currently trying to build a program that extracts information out of a text file and puts it into newly created folders and text files, but it kinda seems bugged. My problem is that I get this error code:

Traceback (most recent call last):   File "C:/Users/Rene/PycharmProjects/draft/prog.py", line 52, in <module>
    new_file = open(next_path, "w+") FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Rene\\PycharmProjects\\draft\\pioranges\\BU raises 040\\SB calls\\BB raises 125\\BU calls\\SB minclicks\\BB calls\\BU minclicks\\SB calls\\BB minclicks\\BU calls\\SB minclicks\\BB calls\\BU minclicks\\SB folds\\BB jams\\BU folds\\40040.1.40125.1.5.1.5.1.5.1.5.1.5.0.3.0.txt'

but it's seemingly random. So when I run my script with all the files I need it processes around 800, some other file set it processes 2000 etc. but when I delete the already processed files it doesn't stop at the file that stopped it previously(even though it always stops at the same file if I don't delete anything), but rather stops at a range of 200-5000 files, which seems completely random to me.

here's my code

import glob
import os

f = glob.glob("*.txt")


for x in range(len(f)):
    file_name = f[0]
    file = open(file_name, "r")
    periods = file_name.count(".")
    next_path = "C:\\Users\\Rene\\PycharmProjects\\draft\\pioranges\\"
    sizes = file_name.split(".")
    postions = ["LJ", "HJ", "CO", "BU", "SB", "BB"]
    first_pos = "BU"
    abc = postions.index(first_pos)
    open_raiser = postions[abc:]
    for i in range(len(sizes)-1):
        if sizes[0] == "0":
            next_path += open_raiser[0] + " folds" + "\\"
            open_raiser.pop(0)
            sizes.pop(0)
        elif sizes[0] == "1":
            next_path += open_raiser[0] + " calls" + "\\"
            sizes.pop(0)
            open_raiser.append(open_raiser[0])
            open_raiser.pop(0)
        elif sizes[0] == "3":
            next_path += open_raiser[0] + " jams" + "\\"
            open_raiser.pop(0)
            sizes.pop(0)
        elif sizes[0] == "5":
            next_path += open_raiser[0] + " minclicks" + "\\"
            sizes.pop(0)
            open_raiser.append(open_raiser[0])
            open_raiser.pop(0)
        else:

            next_path += open_raiser[0] + " raises " + sizes[0][2:] + "\\"
            open_raiser.append(open_raiser[0])
            open_raiser.pop(0)
            sizes.pop(0)
    if not os.path.exists(next_path):
        os.makedirs(next_path)
    lines = [line.rstrip('\n') for line in open(file_name)]
    string = ""
    while len(lines) > 0:
        hand = lines.pop(0) + ":"
        value = lines.pop(0).split(";", maxsplit=1)[0] + ","
        string += hand + value
    next_path += file_name
    print(next_path)
    new_file = open(next_path, "w+")
    new_file.write(string)
    new_file.close()
    f.pop(0)



the files are all 2-5kb big so it shouldn't be a memory problem, really clueless on how I should approach this problem.

Thanks in advance

Upvotes: 0

Views: 79

Answers (1)

randomperson
randomperson

Reputation: 11

found the answer, was due to 260 character limit to file paths

Upvotes: 1

Related Questions