os.listdir()方法
1.方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# 打开文件
path = "/var/www/html/"
dirs = os.listdir( path )
# 输出所有文件和文件夹
for file in dirs:
print (file)
执行以上程序输出结果为:
test.htm
stamp
faq.htm
_vti_txt
robots.txt
itemlisting
2.先写入本地,然后读取本地
import requests
import time
from common.utility import printDict, tryTime
import os
import json
import random
import warnings
warnings.filterwarnings("ignore")
@tryTime(3)
def get_cookies():
if 'token.txt' not in os.listdir(os.getcwd()):
refresh_token= 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjBiYjNiNDRlLTliNGQtNDg3Yy04ZTM3LTFmMjQ0YzVmNDA3M3NpZyJ9.eyJ0cnVzdCI6MTAwLCJpYXQiOjE1ODg3NTg5MjYsImV4cCI6MTYyMDI5NDkyNiwiaXNzIjoib2F1dGgyaWR0IiwianRpIjoiOGE4NzJhODYtMjAxZC00OTg4LTgzMGQtMWQ5Mzc1MGRkMTIzIiwibGF0IjoxNTg4NzU4OTI2LCJhdWQiOiJvYXV0aDJpZHQiLCJjbGkiOiJQYkNSRXVQcjNpYUZBTkVEanRpRXpYb29GbDdtWEdRNyIsInN1YiI6ImQ3MzU2ZmIwLTc4MDAtNDFmMy1hOGEwLWM2MTdkZTY3ODAzOCIsInNidCI6Im5pa2U6cGx1cyJ9.n3NvcD2NuSBozTIBJsFYseZz5GBwb2qVs5gNTdYoBjdKIGTr4Pe9Fj5etFKUjIFrrPaSP16z-qR7QYKlZUkBK6S_JDAPh9U9NbDlsK-KKT-36bEFRq9SttXeSf6KFMz3JGcxW9mml5qdn8CIuU1h2jP2PmktBhbAjlrR758aysuWnvwKScJzI42XcoVjhxDMg8u5hh6Vt8UJsBDNbcTcAairWRLnuFmjr5lT3iN_R061rKlGdmJ30JZirYh7nL3jxGdNOS8NB0d9ckOd1UuBE4TcokxypbJmmTymC69_rpqIxmaz3Lcrx_T5DKTrlSx6MYwnyZk_HBv7gf7kA4Qe8w'
with open('token.txt', 'w', encoding='utf-8') as f:
f.write(refresh_token)
with open('token.txt', 'r', encoding='utf-8') as f:
refresh_token = f.read()
# refresh_token= 'eyJhbGciOiJSUzI1NiIsImtpZCI6IjBiYjNiNDRlLTliNGQtNDg3Yy04ZTM3LTFmMjQ0YzVmNDA3M3NpZyJ9.eyJ0cnVzdCI6MTAwLCJpYXQiOjE1ODg3NTg5MjYsImV4cCI6MTYyMDI5NDkyNiwiaXNzIjoib2F1dGgyaWR0IiwianRpIjoiOGE4NzJhODYtMjAxZC00OTg4LTgzMGQtMWQ5Mzc1MGRkMTIzIiwibGF0IjoxNTg4NzU4OTI2LCJhdWQiOiJvYXV0aDJpZHQiLCJjbGkiOiJQYkNSRXVQcjNpYUZBTkVEanRpRXpYb29GbDdtWEdRNyIsInN1YiI6ImQ3MzU2ZmIwLTc4MDAtNDFmMy1hOGEwLWM2MTdkZTY3ODAzOCIsInNidCI6Im5pa2U6cGx1cyJ9.n3NvcD2NuSBozTIBJsFYseZz5GBwb2qVs5gNTdYoBjdKIGTr4Pe9Fj5etFKUjIFrrPaSP16z-qR7QYKlZUkBK6S_JDAPh9U9NbDlsK-KKT-36bEFRq9SttXeSf6KFMz3JGcxW9mml5qdn8CIuU1h2jP2PmktBhbAjlrR758aysuWnvwKScJzI42XcoVjhxDMg8u5hh6Vt8UJsBDNbcTcAairWRLnuFmjr5lT3iN_R061rKlGdmJ30JZirYh7nL3jxGdNOS8NB0d9ckOd1UuBE4TcokxypbJmmTymC69_rpqIxmaz3Lcrx_T5DKTrlSx6MYwnyZk_HBv7gf7kA4Qe8w'
client_id= f'PbCREuPr3iaFANEDjtiEzXooFl7mXGQ7'
data = {
'refresh_token': refresh_token,
'client_id': client_id,
'grant_type': "refresh_token",
}
ua_list = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.514.0 Safari/534.7 ",
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.55 Safari/534.3 ",
]
headers = {
"Content-Type": "application/json",
"user-agent": random.choice(ua_list)
# "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36",
}
res = requests.post('https://unite.nike.com/', json=data, headers=headers, verify=False)
# print(res.status_code)
token_result = json.loads(res.text)
for tk, tv in token_result.items():
if tk == 'access_token':
with open('token.txt', 'w', encoding='utf-8') as f:
f.write(tv)
for k, v in res.cookies.items():
if k == '_abck':
_abck_str = v
elif k == 'bm_sz':
bm_sz_str = v
return _abck_str,bm_sz_str
if __name__ == '__main__':
print(get_cookies())
os.getcwd()方法
1.返回当前进程的工作目录
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# 切换到 "/var/www/html" 目录
os.chdir("/var/www/html" )
# 打印当前目录
print ("当前工作目录 : %s" % os.getcwd())
# 打开 "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )
# 使用 os.fchdir() 方法修改目录
os.fchdir(fd)
# 打印当前目录
print ("当前工作目录 : %s" % os.getcwd())
# 关闭文件
os.close( fd )
执行以上程序输出结果为:
当前工作目录 : /var/www/html
当前工作目录 : /tmp