Jump to content

Python getting texts from particular parts of a file

Wictorian

So I am making an app with python and I want to open program/files. 

First I get their location via user input and write it into a text file

So it would be something like this

steam : C:\Users\user\program files\steam\steam.exe

origin : C:\Users\user\program files\origin\origin.exe

(not exact location, just made it up for demostration)

So then according to user input I wanna get a file's location 

say ı have steam and I wanna get C:\Users\user\program files\steam\steam.exe

how do I get it?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, James Evens said:

XML is overkill but there are more then enough tutorials for it..

yeah, it is overkill. Cant I just do it with python? because I think it is doable.

I followed this tutorial to write data to a .txt file :https://www.guru99.com/reading-and-writing-files-in-python.html

I just dont know how to call data out of the file. Like each character would have an index number in a list, but in a txt file?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Wictorian said:

do I need to install something

other than pip install json?

Nope, just install the json package and you're good to go.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Souffle said:

Nope, just install the json package and you're good to go.

And how do I store the data?

In a .py file with json?

edit: oh it is actually built in

Link to comment
Share on other sites

Link to post
Share on other sites

screw this, how can I reach a variable from another .py file?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Wictorian said:

And how do I store the data?

In a .py file with json?

You'll need to store it in a separate .json file.  You should have your program.py file and a data.json file (you can name them what you want), you can store it where you want but I'd recommend just storing it alongside the program.py file.

 

Really basic saving/storage:

import json

# load data
with open('data.json', 'r') as file:
  data = json.load(file)

# create new Key-Value pair
data['discord'] = 'PATH_TO_DISCORD'

# save data
with open('data.json', 'w') as file:
  json.dump(data, file)

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Wictorian said:

screw this, how can I reach a variable from another .py file?

main.py:

from otherfile import hello

print(hello)

otherfile.py:

hello = "world"

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Souffle said:

main.py:


from otherfile import hello

print(hello)

otherfile.py:


hello = "world"

otherfile or otherfile.py?

creating and writing to that is the same wih .txt ,right?

also I will convert this to.exe, then it would still work, right?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Wictorian said:

otherfile or otherfile.py?

creating and writing that is the same wih .txt ,right?

also I will convert this to.exe, then it woudl still work, right?

You don't need to specify that it is a .py file, Python already knows this so you can just use otherfile without the .py.

 

Yes, it works the same way.

 

I'm not entirely sure if this will work the same way if you convert it to an executable.  I see no reason why it wouldn't, but I also don't know why you want to turn it into an executable.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Souffle said:

, but I also don't know why you want to turn it into an executable.

because I want to put in taskbar and you cant put .py files

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/8/2020 at 4:59 AM, James Evens said:

It is possible but just pointing at tutorials is the fast answer as you gave zero input. Like not even "reading and writing files in pyhton" google search  result code. Otherwise the hint might haven been CSV which are much simpler then json and XML.

One option is just keep reading the stream until you hit a line end and define this section as string. Alternatively write a PDA to handle the stream. Other option look up if there is a function which returns the file content as array or gives them as lines.

I will just store the data in another .py file

Link to comment
Share on other sites

Link to post
Share on other sites

On 8/8/2020 at 4:28 AM, Souffle said:

main.py:


from otherfile import hello

print(hello)

otherfile.py:


hello = "world"

 

Sorry for late reply but must this import be at the top or can I write it wherever I want?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×