I searched including overseas sites, but I couldn't find a valid solution.
Could you lend me some advice?
Operating System: Windows
When you create a directory using os.madeirs(), it takes "a little" time for the directory to actually be created.
Of course, we use os.path.isdir() to check if the directory has already been created before transferring files.
Depending on the PC specification, there is a 50% chance that the processing will go ahead of directory generation and you will be caught in the check net.
As a provisional measure, we buried time.sleep(2) immediately after os.madeirs, and then proceeded with the follow-up process, which was resolved superficially, but not fundamentally.
So, is there any way to check if the directory has been created, or if it has been successfully created?
Also, is it possible to make a decision not only for os.madeirs, but also for operations that take a long time to work on the OS side apart from script processing so that the return value is received before proceeding with the subsequent processing?
Directory generation is done both locally and FTP destination.If you know, could you also tell me if there is a way to pick up the end-of-directory status when you are connecting with ftplib.FTP (both WindowsPCs)?
[Additional note]
Thank you for your comment.The source code is as follows:
try:
ifos.path.isdir(self.aa):
shutil.rmtree (self.aaa)
os.madeirs (self.aa)
time.sleep(2)
except Exception as:
print e
try:
ifos.path.isdir(self.aa):
# file copy processing
else:
logger.info(u"I haven't generated it yet")
except Exception as:
print e
When os.madeirs returns, the directory may only be cached.
In this case, have you deleted a large amount of data in the last instance?In that case, the cache is finished and the return value is returned, but the disk itself is not finished.Because the directory name you are trying to write to is the same as the directory name you are deleting, if you do not complete the deletion process, the process will conflict, so you may encounter an error.
Why don't you change the directory name instead of deleting it and delete it later?The rename process is fast.
try:
ifos.path.isdir(self.aa):
os.rmname(self.aaa, self.aaa+'_bak')
os.madeirs (self.aa)
except Exception as:
print e
try:
# file copy processing
except Exception as:
print e
ifos.path.isdir(self.aaa+'_bak'):
shutil.rmtree(self.aaa+'_bak')
I didn't have a Windows environment, so I tried it on Linux, but there was no time lag for the directory to be created as shown below.
Please try this code in your environment and let me know the result.
$cat sample.py
#!/usr/bin/env python
importos
import shutil
fname = "aaa"
fpath=os.getcwd()+os.path.sep+fname
try:
ifos.path.isdir(fpath):
shutil.rmtree (fpath)
os.madeirs (fpath)
printos.path.isdir(fpath)
except Exception as:
print e
$ python sample.py
True
$ python sample.py
True
$ python sample.py
True
$ python sample.py
True
$ python sample.py
True
$
The python version is as follows:
$python --version
Python 2.7.4
$
© 2024 OneMinuteCode. All rights reserved.