Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何关闭在脚本外部打开的.csv文件?_Python_Python 3.x_File_Window - Fatal编程技术网

Python 如何关闭在脚本外部打开的.csv文件?

Python 如何关闭在脚本外部打开的.csv文件?,python,python-3.x,file,window,Python,Python 3.x,File,Window,我目前正在使用Raspian OS和Libre Office。我正在使用Python 3。 我正在尝试创建一个RFID门锁,记录每个进入的用户 我遇到的问题是,如果有人在查看日志后打开.csv文件,我的脚本将无法写入该文件。所以它抛出了一个错误。我希望我的脚本关闭当前打开的实例(窗口)并打开该文件,然后我可以打开脚本并写入该文件 该文件与我的python脚本位于同一文件夹中 我试过: os.system("TASKKILL /IM Entry_Log.csv") 没有运气 这就是我现在拥有的功

我目前正在使用Raspian OS和Libre Office。我正在使用Python 3。 我正在尝试创建一个RFID门锁,记录每个进入的用户

我遇到的问题是,如果有人在查看日志后打开.csv文件,我的脚本将无法写入该文件。所以它抛出了一个错误。我希望我的脚本关闭当前打开的实例(窗口)并打开该文件,然后我可以打开脚本并写入该文件

该文件与我的python脚本位于同一文件夹中

我试过:

os.system("TASKKILL /IM Entry_Log.csv")
没有运气

这就是我现在拥有的功能。我需要在我的异常语句中关闭该文件

def unlockDoor(x):
    #Add GPIO Output here later
    while True:
        try:
            userDictionary = shelve.open("User_Dictionary")
            print('Welcome,', userDictionary[x])
            print('Door Unlocked')
            with open("Entry_Log.csv", "a") as log:
                log.write("{0},{1},{2}\n".format(time.strftime("%Y-%m-%d %H:%M:S"), x, userDictionary[x]))
            userDictionary.close()
        except:
            #close the open window for "Entry_Log.csv" here <--
def解锁门(x):
#稍后在此处添加GPIO输出
尽管如此:
尝试:
userDictionary=shelve.open(“用户字典”)
打印('Welcome',userDictionary[x])
打印(“车门已解锁”)
打开(“Entry_Log.csv”、“a”)作为日志:
log.write(“{0},{1},{2}\n.”格式(time.strftime(“%Y-%m-%d%H:%m:S”)、x、userDictionary[x]))
userDictionary.close()
除:

#在这里关闭“Entry_Log.csv”的打开窗口以下代码在Linux上对我有效,使用的是
psutil
包。当文件无法打开时,将进行搜索以查找打开该文件的所有其他进程,并向这些进程发送终止信号

注意:只有当调用进程具有正确的权限时,此操作才有效

import psutil
import sys
import os
import signal

def terminate_others(pattern):
    pids = psutil.pids();
    for pid in pids:
        p = psutil.Process(pid)
        try:
            flist = p.open_files()
            if flist:
                for f in flist:
                    if pattern in f.path:
                        print "Terminating " + repr(pid)
                        os.kill(pid, signal.SIGTERM)
        except:
            pass


fname = "Entry_Log.csv"

try:
    with open(fname, "a") as log:
        log.write("Some log text")
except:
    print "error:", sys.exc_info()[0]
    # close other processes that have this file open
    terminate_others(fname)

希望这能有所帮助。

我非常确定
TASKKILL
是windows命令行命令,而不是linux命令行命令。您可以尝试linux等效命令OK,我将搜索等效命令。有人知道这个等价物是什么吗?
pkill
我想我看到的所有关于
pkill
的东西都表明它是停止其他python脚本,而不是关闭文件。那么
os.kill(pid,sig)
呢。不确定pid和sig需要什么beI肯定会尝试这个,并很快回复您