There is a structure: A -testfile.py B -textfile.txt
In the testfile.py file you need to read all the textfile.txt data
I tried to specify through a relative path, but it did not work my_file = open ("../ b / textfile.txt", "r")
There is a structure: A -testfile.py B -textfile.txt
In the testfile.py file you need to read all the textfile.txt data
I tried to specify through a relative path, but it did not work my_file = open ("../ b / textfile.txt", "r")
If you need to open a file relative to the script being run, you can take the path to it from the built-in variable __file__ :
import os print os.path.join(os.path.dirname(__file__),'..','b','textfile.txt') import os a = 'dir/dd\er//wq\\qwe' n = os.path.join(os.getcwd(), os.path.normpath(a)) print(n) with open(os.path.join(n, 'f1'), 'r') as fr, open(os.path.join(n, 'f2'), 'w') as fw: fw.write(fr.read()) out:
C:\Scripts\python\2016\4\dir\dd\er\wq\qwe Check os.chdir , perhaps the path is not built initially from the path that you mean. well and try to build a relative path through os.path.join
Source: https://ru.stackoverflow.com/questions/569423/
All Articles
pkgutilandpkg_resourcesmodules) - jfs