检查什么程序创建文件
Check what Program creates a file
有什么方法可以查看哪个程序在 Python 中创建或删除了文件?我知道你可以编写一个程序,当文件 created/deleted/modified 使用看门狗时打印出来,但我想知道什么程序 creates/deletes/modifies 这个文件。这在 Python 中可行吗?
import os
os.remove(fiile)
这是删除文件的方式。希望对你有帮助
这是一个很好的问题,可以 使用前面提到的 os module. The os module is used for many things, one of those being directory management and is perfect for what you are asking. The code in the following examples is mine and is referenced from the official documentation 来完成。
正在创建文件
创建文件时必须采取预防措施,以确保您不会损坏或删除已经存在的文件。请仔细阅读以下代码。
"""
First open the file with os.open(). This is used when ever you want to create or modify a
file and is the bread and butter of the following operations.
"""
with open("Desired_file.txt", 'w') as file:
# Actions
file.close()
此时您可能会问:什么是 'w'?这是我们用来打开文件的标志。 w 是在两个主要场景中使用的标志:
1. 当您想创建一个不存在的文件时。
2. 当您想覆盖 一个文件时。
您可能还会问,什么是 file.close()? 关闭功能本质上就像按 ctrl+在你的键盘上。 正在保存文件以确保记录我们保存的任何更改。
向文件添加文本
与第一个示例一样,向文件添加文本是将标志变量从 w
更改为 a
的简单事情。下面是一个例子。
with open("file.txt", "a") as file:
file.append("Note: Add fish to shopping list.")
file.close()
请注意,使用 file.close
是因为我们要 保存我们的更改。
正在读取文件
这又是一个简单的问题,将标志更改为 "a"
。请参阅以下示例中的实际用法。
with open("Desired_file.txt", "r") as file:
# Get the file data with file.read()
file_data = file.read()
file.close()
重要的是要注意,这不是读取文件的好方法,因为如果文件中有行,它将打印 '\n'
作为变量,因为它以列表格式打印信息。为了避免这种情况,我喜欢在 .read() 函数之后使用 .split() 函数。下面是另一个实际使用示例。
with open("Desired_file.txt", "r") as file:
file_data = file.read().split()
file.close()
现在最后一步是读取文件中的每一行。见下文。
with open("Desired_file.txt", "r") as file:
file_data = file.read().split()
for line in file_data:
print(line)
# Note that at this point the file has finished reading the file. Now all that we have to do is save the file
file.close()
我真的希望这对您有所帮助。如果您有任何问题,我将非常乐意提供帮助。很抱歉,我没有最好的方式来解释事情,这可能会更加简化,但我希望它具有足够的可读性。
有什么方法可以查看哪个程序在 Python 中创建或删除了文件?我知道你可以编写一个程序,当文件 created/deleted/modified 使用看门狗时打印出来,但我想知道什么程序 creates/deletes/modifies 这个文件。这在 Python 中可行吗?
import os
os.remove(fiile)
这是删除文件的方式。希望对你有帮助
这是一个很好的问题,可以 使用前面提到的 os module. The os module is used for many things, one of those being directory management and is perfect for what you are asking. The code in the following examples is mine and is referenced from the official documentation 来完成。
正在创建文件
创建文件时必须采取预防措施,以确保您不会损坏或删除已经存在的文件。请仔细阅读以下代码。
"""
First open the file with os.open(). This is used when ever you want to create or modify a
file and is the bread and butter of the following operations.
"""
with open("Desired_file.txt", 'w') as file:
# Actions
file.close()
此时您可能会问:什么是 'w'?这是我们用来打开文件的标志。 w 是在两个主要场景中使用的标志:
1. 当您想创建一个不存在的文件时。
2. 当您想覆盖 一个文件时。
您可能还会问,什么是 file.close()? 关闭功能本质上就像按 ctrl+在你的键盘上。 正在保存文件以确保记录我们保存的任何更改。
向文件添加文本
与第一个示例一样,向文件添加文本是将标志变量从 w
更改为 a
的简单事情。下面是一个例子。
with open("file.txt", "a") as file:
file.append("Note: Add fish to shopping list.")
file.close()
请注意,使用 file.close
是因为我们要 保存我们的更改。
正在读取文件
这又是一个简单的问题,将标志更改为 "a"
。请参阅以下示例中的实际用法。
with open("Desired_file.txt", "r") as file:
# Get the file data with file.read()
file_data = file.read()
file.close()
重要的是要注意,这不是读取文件的好方法,因为如果文件中有行,它将打印 '\n'
作为变量,因为它以列表格式打印信息。为了避免这种情况,我喜欢在 .read() 函数之后使用 .split() 函数。下面是另一个实际使用示例。
with open("Desired_file.txt", "r") as file:
file_data = file.read().split()
file.close()
现在最后一步是读取文件中的每一行。见下文。
with open("Desired_file.txt", "r") as file:
file_data = file.read().split()
for line in file_data:
print(line)
# Note that at this point the file has finished reading the file. Now all that we have to do is save the file
file.close()
我真的希望这对您有所帮助。如果您有任何问题,我将非常乐意提供帮助。很抱歉,我没有最好的方式来解释事情,这可能会更加简化,但我希望它具有足够的可读性。