使用python输出txt格式的测试文件,自定义文件大小

def create_large_text_file(file_path, file_size_mb):
    target_size_bytes = file_size_mb * 1024 * 1024 * 1024   # 计算文件大小

    with open(file_path, 'w') as f:
        while f.tell() < target_size_bytes:
            f.write("This is a sample line of text.\n")

if __name__ == "__main__":
    output_file_path = "D:/large_file.txt"  # 指定保存路径
    desired_file_size_mb = 1    # 指定文件大小,单位为GB

    create_large_text_file(output_file_path, desired_file_size_mb)
    print(f"File '{output_file_path}' created with a size of {desired_file_size_mb} GB.")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽力回答你的问题。首先,我需要确认一下你的问题是关于Python编写队列类的实现和使用方面的,对吗? 如果是的话,我可以给你提供一份基本的Python代码,实现一个队列类,包括入队、出队、修改队列大小、判断队列是否为空或者已满等功能。同时,我也会告诉你如何在另一个文件使用这个类,并提供一个入口函数,用于测试类的各个方法。 以下是示例代码: ### queue.py ### import threading import time class Queue: def __init__(self, max_size): self.max_size = max_size self.queue = [] self.lock = threading.Lock() self.not_full = threading.Condition(self.lock) self.not_empty = threading.Condition(self.lock) def push(self, item, timeout=None): with self.not_full: if timeout is None: while len(self.queue) == self.max_size: self.not_full.wait() else: end_time = time.time() + timeout while len(self.queue) == self.max_size: remaining = end_time - time.time() if remaining <= 0.0: raise Exception("queue is full") self.not_full.wait(remaining) self.queue.append(item) self.not_empty.notify() def pop(self, timeout=None): with self.not_empty: if timeout is None: while len(self.queue) == 0: self.not_empty.wait() else: end_time = time.time() + timeout while len(self.queue) == 0: remaining = end_time - time.time() if remaining <= 0.0: raise Exception("queue is empty") self.not_empty.wait(remaining) item = self.queue.pop(0) self.not_full.notify() return item def resize(self, new_size): with self.lock: self.max_size = new_size def is_empty(self): with self.lock: return len(self.queue) == 0 def is_full(self): with self.lock: return len(self.queue) == self.max_size ### main.py ### from queue import Queue def test_queue(): q = Queue(5) q.push(1) q.push(2) q.push(3) q.push(4) q.push(5) assert q.is_full() == True try: q.push(6, timeout=2.0) except Exception as e: print(e) assert q.pop() == 1 assert q.pop() == 2 assert q.pop() == 3 assert q.pop() == 4 assert q.pop() == 5 assert q.is_empty() == True try: q.pop(timeout=2.0) except Exception as e: print(e) q.resize(10) q.push(1) q.push(2) assert q.is_full() == False if __name__ == '__main__': test_queue() 在上面的代码中,我们定义了一个Queue类,实现了入队、出队、修改队列大小、判断队列是否为空或者已满等功能。我们使用Python的线程锁和条件变量来保证队列的线程安全性。 在main.py文件中,我们编写了一个测试函数test_queue(),用于测试Queue类的各个方法。我们在这个函数中创建了一个Queue对象,进行了入队、出队、修改队列大小、判断队列是否为空或者已满等操作,并使用assert语句进行了断言,确保Queue类的各个方法能够正常工作。 最后,在main.py文件的末尾,我们使用if __name__ == '__main__'语句,调用test_queue()函数,用于测试Queue类的各个方法。当我们运行main.py文件时,就会自动执行test_queue()函数,输出测试结果。 希望这份代码能够对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wghjpojg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值