0

So I have a program right now that reads text files from a folder. They are in a separate folder than the one my program is in, and both of them are in a dropbox folder together. Right now if I want to read one of these .txt files I'll do

file='/Users/JohnDoe/application/folder/file.txt'

text=open(file,'r').read()

print(text)

and it works just fine on my pc. The problem is that if I open my dropbox on another computer and run this program, it won't work because the filepath will probably be slightly different. Is there a way to work around this? Maybe a module that searches for files using something other than filepath, or something like that.

I'm brand new to python and to programming in general so any suggestions are appreciated. Thanks.

rrf5067
  • 1
  • 1

1 Answers1

0

If you know the specific name of the file, but you're unsure where it might be located on another machine, you can use os.walk like this previous answer suggests. Or you can read up on using regex (regular expressions) in Python like this previous answer suggests.

Feernot
  • 34
  • 8