Help with converting a program from windows file path system to replit

I started this code on thonny on my computer, but would like to transfer onto replit but im having a hard time getting the file pathways to work

import os
from pathlib import Path
directory=""
name = ""
health = 0
damage = 0
critchance = 0
armour = 0
xp = 0
level = 0
gold = 0
inventory = []
st = 0
spe = 0
crt = 0
end = 0
strskill=["Brawler"]


def startup():
    startup=input("Do you want to load a game(1) or start a new one(2): ").strip()
    if startup =="2":
        directory=input("What do you want the save file to be called: ")
        parent_dir = Path.cwd()
        path = os.path.join(parent_dir, directory)
        os.mkdir(path)
        file=open(f"{directory}\Player Data.txt", 'w')
        name=input("What would you like to be called: ")
        file.write(f"name={name}"+"\n"+"health=20"+"\n"+"damage=5"+"\n"+"critchance=10"+"\n"+"armour=0"+"\n"+"xp=0"+"\n"+"level=0"+"\n"+"gold=5")
        file.close()
        parent_dir = (f"{Path.cwd()}\{directory}")
        path = os.path.join(parent_dir, "SkillTree")
        os.mkdir(path)
       
       
        parent_dir = (f"{Path.cwd()}\{directory}\SkillTree")
        path = os.path.join(parent_dir, "Strength")
        os.mkdir(path)
        file=open(f"{Path.cwd()}\{directory}\SkillTree\Strength\{strskill[0]}.txt","a")
        file.write(("""level=0
                    max level=5
                    effect=0.15
                    name="Brawler"
                    escrption="+15% damage to melee weapons"
                    skillrequired="nothing"""))
        file.close()
       
       
        parent_dir = (f"{Path.cwd()}\{directory}\SkillTree")
        path = os.path.join(parent_dir, "Endurance")
        os.mkdir(path)
       
       
        parent_dir = (f"{Path.cwd()}\{directory}\SkillTree")
        path = os.path.join(parent_dir, "Magic")
        os.mkdir(path)
   


   
   
   
    if startup == "1":
        savefile=input("Enter name of save file: ")
        directory=savefile

def update_variable(file_path, variable_name, new_value):
    with open(f"{Path.cwd()}\{directory}\Player Data.txt", 'r') as file:
        lines = file.readlines()
    for i in range(len(lines)):
        if variable_name in lines[i]:
            lines[i] = f"{variable_name}={new_value}\n"
            break
    with open(f"{Path.cwd()}\{directory}\Player Data.txt", 'w') as file:
        file.writelines(lines)


update_variable("Player Data.txt", "critchance", "11")


with open(f"{Path.cwd()}\{directory}\Player Data.txt", "r") as file:
    for line in file:
        variable, value = line.strip().split('=')
        value = value.strip().strip('"')
       
        if variable == "name":
            name = value
        elif variable == "health":
            health = int(value)
        elif variable == "damage":
            damage = int(value)
        elif variable == "critchance":
            critchance = int(value)
        elif variable == "armour":
            armour = int(value)
        elif variable == "xp":
            xp = int(value)
        elif variable == "level":
            level = int(value)
        elif variable == "gold":
            gold = int(value)


startup()

Welcome to Ask @ThomasDriver1!

Windows uses backward slashes (\) instead of forward slashes (/) for files. To change this, you can press CTRL + F and type \, then press the Select All button, then press Backspace and /.

1 Like

Thank you this seems to have fixed one of the issues. but i cant get it to create its own folder and text files still?

Try using the 'x' argument when creating files:

file = open('/folder/file.py', 'x') # creates the file in the folder

Hope this helps!

An easier way:

file = open("./folder/file.py", "w+") 
# creates the file in the folder and sets it for writing

Also, I doubt /folder/file.py would work, as it’d most likely try create the folder in the root directory of the filesystem instead of the CWD [1]


  1. current working directory - the folder the program is running in ↩︎

2 Likes

thank i will try this

new problem its not able to split my text file using the line

variable, value = line.strip().split('=')
the text file is 
name=""
health=0
damage=0
critchance=0
armour=0
xp=0
level=0
gold=0
st=0
spe=0
crt=0
end=0

the error is

Traceback (most recent call last):
  File "/home/runner/Texta-dventure-game/main.py", line 87, in <module>
    variable, value = line.strip().split('=')
ValueError: not enough values to unpack (expected 2, got 1)

split returns a list, which means you can’t store it in multiple variables like that.

You seem to be using a TOML/INI file format, so I would recommend using the Python package toml (toml · PyPI):

import toml

# Load the file into a dictionary
with open('./path/to/file', 'r') as file:
     user_data = toml.loads(file.read())

print(f"Hello, {user_data.name}!")

# Saving the dictionary back to the file:
with open('./path/to/file', 'w') as file:
     toml.dump(user_data, file)
3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.