0

I'm using Python 3.6 and I'm wondering.. How can I locate my files in Python so I can use the Open() and Read() function.

Your help would be a great help for me since I'm still new

Example.... If I'm running a python script from C:\Python\Exercise .

How can I run another file using open and read function that is located in

another directory, e.g. C:\Users\Example\mytext.txt

  • if you know path then you can use `open("C:\\Users\\Example\\mytext.txt")` – furas Dec 18 '20 at 13:25
  • Thanks, but how if I want to search a file and locate it? – Juan Ninaso Dec 18 '20 at 13:27
  • if you need access to user folder then you can try `os.getenv('HOME')` and then you can appedn rest of path - `os.path.join( os.getenv('HOME'), 'Example\\mytext.txt' )` – furas Dec 18 '20 at 13:28
  • if you want to search then you have to write code for this - you can use `os.walk(folder)` to get filenames and dirnames from all subfolders in `folder`, eventually you can use ie `glob.glob('*/*.txt')` to get all `.txt` files in all subfolders. – furas Dec 18 '20 at 13:30
  • @furas I really appreciate your hard work and helps! Thank you furas! – Juan Ninaso Dec 18 '20 at 13:31

0 Answers0