Как лучше всего попытаться открыть файл и повторить попытку через n секунд?
В настоящее время я делаю:
import os
from os import path
import shutil
dir_path = path.abspath(path.join("foo", "bar"))
destination_path = path.abspath(path.join("foo", "dest_dir"))
for f in dir_path:
try:
# try to open the file if the file isn't used any longer
file_opened = open(f, 'r')
# move the file on successful opening
shutil.move(file_opened, destination_path)
file_opened.close()
except IOError:
return False
Итак, на данный момент я не обрабатываю исключение. Я думаю о создании дополнительной функции для открытия файла и вызова функции для исключения с помощью time.sleep(n)
Однако я уверен, что должно быть что-то еще...
я не использую
with open(f, 'rb') as file_opened:
do whatever`
РЕДАКТИРОВАТЬ:
Один процесс создает файл, и я хочу, чтобы процесс Python переместил файл, как только я буду уверен, что запись/создание файла завершено. Итак, я добавил Shutil.move в приведенный выше код, чтобы показать всю ситуацию.
РЕДАКТИРОВАТЬ:
Пожалуйста, найдите ниже код, который я разработал для решения проблемы. Я закончил тем, что написал собственное решение для его обработки:
import os
from os import path
import psutil
from retry import retry
import shutil
from subprocess import check_output,Popen, PIPE
import glob
import time
class FileHandler:
def __init__(self, fn_source, dir_source):
self.file_source_name = fn_source
self.file_source_path = path.join(dir_source, self.file_source_name)
self.dir_dest_path = path.join(dir_source, "test")
self.file_dest_path = path.join(self.dir_dest_path, self.file_source_name)
def check_file(self):
if os.path.exists(self.file_source_path):
try:
os.rename(self.file_source_path, self.file_source_path)
print("file renamed")
return True
except:
print("can not rename the file..retrying")
time.sleep(1)
self.check_file()
else:
print("source file does not exist...retrying")
time.sleep(5)
self.check_file()
def check_destination(self):
if os.path.exists(self.file_source_path) and not os.path.exists(self.file_dest_path):
return True
elif os.path.exists(self.file_source_path) and os.path.exists(self.file_dest_path):
try:
print(self.file_dest_path, self.file_source_name)
os.remove(self.file_dest_path)
return True
except Exception as e:
print("can not remove the file..retrying")
time.sleep(5)
self.check_destination()
def move_file(self):
if self.check_destination():
print(self.file_source_path)
shutil.move(self.file_source_path, self.file_dest_path)
print("moved", str(self.file_source_path))
return True
else:
print("can not move the file..retrying")
time.sleep(1)
self.move_file()
def file_ops(self):
if self.check_file():
self.move_file()
else:
print("source does not exist")
time.sleep(1)
self.file_ops()
return True
def each_file_ops(fn, dir_source):
fh = FileHandler(fn, dir_source)
return fh.file_ops()
def main(dir_source):
dir_files = glob.glob(path.join(dir_source, '*.txt'))
if dir_files:
[each_file_ops(f, dir_source) for f in dir_files]
else:
print("source dir is empty")
time.sleep(1)
main(dir_source)
if __name__ == '__main__':
main(path.join(""))
file_path
? Когда вы используете переменную циклаf
? - person schwobaseggl   schedule 21.06.2018file not available
(если файл на самом деле недоступен). Для файла можно установить несколько блокировок чтения. Проблема может возникнуть, если вы пытаетесь открыть файл в режиме добавления или записи, в этом случае вам придется подождать, пока предыдущий пользователь не снимет блокировку (при условии, что файл уже открыт в режиме записи/добавления). - person Umesh Kaushik   schedule 21.06.2018