python實(shí)現(xiàn)不同文件夾下的函數(shù)相互調(diào)用
python不同文件夾下的函數(shù)相互調(diào)用
加上以下三句代碼
import os
import sys
sys.path.append('../想調(diào)用的文件所在的文件夾名/')python調(diào)用其他文件夾下文件中的函數(shù)
跟大家分享下python如何調(diào)用其他文件夾下的函數(shù)
1.在項(xiàng)目下新建文件夾common
并在該目錄下創(chuàng)建get_token.py文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import yaml
import os
# cur = os.path.dirname(os.path.realpath(__file__))
def get_token():
# 從配置文件中讀取token值,并返回
p = os.path.join(r'D:\autotest\api\628x\Token.yaml')
f = open(p)
a = f.read()
t = yaml.load(a, Loader=yaml.FullLoader)
f.close()
return t["token"]
if __name__ == "__main__":
get_token()2.在其他文件中調(diào)用common文件夾下
get_token.py文件中的get_token()函數(shù)

from common.get_token import get_token
導(dǎo)入改模塊后就可以直接使用get_token()這個(gè)函數(shù)了
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 用pip3命令安裝
import requests
from ruamel import yaml
import json
from common.get_token import get_token
def test_collectionList():
host = 'http://47.96.169.xx:8081/'
url = host + "api/collection/list"
headers = {'Content-Type': 'application/json', "x-token": get_token()}
# 初始化url請(qǐng)求對(duì)象
response = requests.get(url=url, headers=headers)
print(response.json())
assert response.status_code == 200
if __name__ == "__main__":
test_collectionList()總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 實(shí)現(xiàn)多進(jìn)程日志輪轉(zhuǎn)ConcurrentLogHandler
這篇文章主要介紹了python 實(shí)現(xiàn)多進(jìn)程日志輪轉(zhuǎn)ConcurrentLogHandler,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
Python調(diào)用C語(yǔ)言動(dòng)態(tài)庫(kù)的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了Python調(diào)用C語(yǔ)言動(dòng)態(tài)庫(kù)的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2024-12-12
python腳本作為Windows服務(wù)啟動(dòng)代碼詳解
本篇文章給大家分享了用python腳本寫(xiě)出作為Windows服務(wù)啟動(dòng)功能,對(duì)此有需求的朋友跟著小編一起學(xué)習(xí)下。2018-02-02
解決Python print輸出不換行沒(méi)空格的問(wèn)題
今天小編就為大家分享一篇解決Python print輸出不換行沒(méi)空格的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

