python - 将变量发送到另一个函数的问题
问题描述
- 调用函数什么检查文件内容、大小等
- 如果文件被文件大小更改,则调用函数,如果文件大小更改检查文件内容,将更改添加到列表或从列表中删除问题如何在其他函数中正确使用变量?
import re
import os
from time import sleep
hosts_file = "hosts.txt"
class Hosts(object):
def __init__(self):
self.hosts_file = hosts_file
def get_file_size(self):
f_size = os.stat(self.hosts_file)
return f_size.st_size
def work_with_hosts_file():
host_file_work = Hosts()
file_size = host_file_work.get_file_size()
return file_size # use this var
def compare_hosts_file_size(): # in this function
host_file_work = Hosts()
file_size_check = host_file_work.get_file_size()
if file_size != file_size_check:
#do stuff
if __name__ == "__main__":
work_with_hosts_file()
get_connection_hosts_info()
while True:
compare_hosts_file_size()
sleep(5.0)
先感谢您!
解决方案
您需要将 的返回值分配work_with_hosts_file()
给一个变量,然后将其作为参数传递给compare_hosts_file_size()
.
import re
import os
from time import sleep
hosts_file = "hosts.txt"
class Hosts(object):
def __init__(self):
self.hosts_file = hosts_file
def get_file_size(self):
f_size = os.stat(self.hosts_file)
return f_size.st_size
def work_with_hosts_file():
host_file_work = Hosts()
file_size = host_file_work.get_file_size()
return file_size # use this var
def compare_hosts_file_size(file_size): # in this function
host_file_work = Hosts()
file_size_check = host_file_work.get_file_size()
if file_size != file_size_check:
#do stuff
if __name__ == "__main__":
size = work_with_hosts_file()
get_connection_hosts_info()
while True:
compare_hosts_file_size(size)
sleep(5.0)
推荐阅读
- charts - Primefaces 6.2 条形图数据提示格式
- node.js - 每个节点线程应该创建多少个消费者实例?
- oauth-2.0 - OAuth2 隐式流程 - IFrame 刷新身份
- python - 为什么 Swift 中的 FFT 与 Python 中的不同?
- javascript - 将 Javascript ES6 Map() 传递给 MVC 控制器
- python - Django allauth 社交应用程序额外数据
- javascript - 获取 [innerHtml] 中的子 html 元素 | 用于突出显示文本的 Angular 6
- html - cPanel HTML 无法打开网站:403 Forbidden
- javascript - 从前端 AJAX 向节点 Js 中的给定路由发出 404/405 错误。本地主机 URL 不起作用
- rust - 在 Rust 中的线程之间共享只读对象?