将鼠标位置写入.txt文件 [英] write mouse position to .txt file

查看:60
本文介绍了将鼠标位置写入.txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小项目是将坐标写入文件,而我的代码如下所示:

My little project is to write coordinates to file and my code look like this:

import pyautogui
import time

c = open("coord.txt", "w")
while True:
        x, y = pyautogui.position()
        positionStr = str(x).rjust(4) + str(y).rjust(4)
        print(positionStr)
        c.write(positionStr)
        time.sleep(1)

不起作用,因为它在终端中显示了坐标,但是文件coord.txt仍然为空

It not work becouse it show in terminal the coordinates but file coord.txt is still empty

推荐答案

在循环中您只是缺少 flush . write 方法不一定会将数据写入磁盘.您必须调用 flush 方法以确保发生这种情况.

You are just missing to flush inside the loop. The write method doesn't necessarily write the data to disk. You have to call the flush method to ensure this happens.

import pyautogui
import time

c = open("coord.txt", "w")
while True:
    x, y = pyautogui.position()
    positionStr = str(x).rjust(4) + str(y).rjust(4)
    print(positionStr)
    c.write(positionStr + '\n')
    c.flush()
    time.sleep(1)

最好将 c = open("coord.txt","a")替换为并用open("coord.txt","w")替换为c:,所以当循环结束时,文件会自动关闭,否则您需要调用 close .

Is better to replace c = open("coord.txt", "a") with with open("coord.txt", "w") as c: so when the loop end the file automatically close otherwise you need to call close.

import pyautogui
import time

with open("coord.txt", "w") as c:
    while True:
        x, y = pyautogui.position()
        positionStr = str(x).rjust(4) + str(y).rjust(4)
        print(positionStr)
        c.write(positionStr + '\n')
        c.flush()
        time.sleep(1)

您还可以使用 print()函数写入文件:

You can also use the print() function to write to the file:

import pyautogui
import time

with open("coord.txt", "w") as c:
    while True:
        x, y = pyautogui.position()
        positionStr = str(x).rjust(4) + str(y).rjust(4)
        
        print(positionStr)
        print(positionStr, file=c, flush=True)

        time.sleep(1)

这是因为 print()被定义为 print(object(s),sep = separator,end = end,file = file,flush = flush)其中:

This is because the print() is defined as print(object(s), sep=separator, end=end, file=file, flush=flush) where:

  • objects :任何对象,并且可以根据需要进行选择.在打印之前将转换为字符串
  • sep ='separator':可选.如果存在多个对象,请指定如何分离对象.默认值为''
  • end ='end':可选.指定末尾要打印的内容.默认值为'\ n'(换行符)
  • 文件:可选.具有write方法的对象.默认值为 sys.stdout
  • flush :可选.一个布尔值,指定输出是刷新的( True )还是缓冲的( False ).默认值为 False
  • object(s): Any object, and as many as you like. Will be converted to string before printed
  • sep='separator': Optional. Specify how to separate the objects, if there is more than one. Default is ' '
  • end='end': Optional. Specify what to print at the end. Default is '\n' (line feed)
  • file: Optional. An object with a write method. Default is sys.stdout
  • flush: Optional. A Boolean, specifying if the output is flushed (True) or buffered (False). Default is False

这篇关于将鼠标位置写入.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆