版权声明:本文为博主原创文章,如需转载请贴上原博文链接:https://blog.csdn.net/u011628215/article/details/106719350
最近在研究扫描文件夹里面的文件并进行解析,例如:扫描文件夹中是否出现新增的文件(text文本等),需要得到该文件的路径(即该文件所在文件夹的地址)。
因为我使用了三个.py文件:第一个py文件命名为main.py,第二个py文件命名为:test1.py,第三个py文件命名为:test2.py,其中main.py文件是主程序,test1、test2都是函数供main文件调用。我的目的是想要运行主程序main.py文件,同时分别得到三个文件的路径。三个文件的路径如下:
main.py E:\Python\Test
test1.py E:\Python\Test\folder1
test2.py E:\Python\Test\folder2
先给出三个文件的程序及运行后的结果:
# === test1.py test1的程序 ===
import os
print('folder1:', os.getcwd())
# test1.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/folder1/test1.py', wdir='E:/Python/Test/folder1')
folder1: E:\Python\Test\folder1
# --------------------------------------------
# === test2.py test2的程序 ===
import os
print('folder2:', os.getcwd())
# test2.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/folder2/test2.py', wdir='E:/Python/Test/folder2')
folder2: E:\Python\Test\folder2
# --------------------------------------------
# === main.py 主程序 ===
import os
from folder1 import test1
from folder2 import test2
print('main:', os.getcwd())
# main.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/main.py', wdir='E:/Python/Test')
folder1: E:\Python\Test
folder2: E:\Python\Test
main: E:\Python\Test
结果显而易见:最后得出的三个文件的路径都是“E:\Python\Test”。
给出三个文件的程序及运行后的结果:
# === test1.py test1的程序 ===
import sys
print('folder1:', sys.path[0])
# test1.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/folder1/test1.py', wdir='E:/Python/Test/folder1')
folder1: E:\Python\Test\folder1
# --------------------------------------------
# === test2.py test2的程序 ===
import sys
print('folder2:', sys.path[0])
# test2.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/folder2/test2.py', wdir='E:/Python/Test/folder2')
folder2: E:\Python\Test\folder2
# --------------------------------------------
# === main.py 主程序 ===
import sys
from folder1 import test1
from folder2 import test2
print('main:', sys.path[0])
# main.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/main.py', wdir='E:/Python/Test')
folder1: E:\Python\Test
folder2: E:\Python\Test
main: E:\Python\Test
结果显而易见:和失败方法1一样,最后得出的三个文件的路径都是“E:\Python\Test”。
给出三个文件的程序及运行后的结果:
# === test1.py test1的程序 ===
import os
print('folder1:', os.path.split(__file__)[0])
# test1.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/folder1/test1.py', wdir='E:/Python/Test/folder1')
folder1: E:/Python/Test/folder1
# --------------------------------------------
# === test2.py test2的程序 ===
import os
print('folder2:', os.path.split(__file__)[0])
# test2.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/folder2/test2.py', wdir='E:/Python/Test/folder2')
folder2: E:\Python\Test\folder2
# --------------------------------------------
# === main.py 主程序 ===
import os
from folder1 import test1
from folder2 import test2
print('main:', os.path.split(__file__)[0])
# main.py运行结果:
Python 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)] on win32
>>>runfile('E:/Python/Test/main.py', wdir='E:/Python/Test')
folder1: E:\Python\Test\folder1
folder2: E:\Python\Test\folder2
main: E:/Python/Test
终于达到想要的结果了,调用主程序,分别都能返回各个文件所在的路径。
解决这个问题的关键点,还在于发现每个程序在使用console运行后,在Special Variables中第一行就出现了__file__参数,这个参数就是各个文件的路径+文件名,利用这个一点再结合os.path.split()函数就搞定啦!