英文:
Python and temporary files when moved to temp path they are not considered files anymore
问题
以下是您要翻译的代码部分:
We've created a temporary directory within a python function to move files into for processing:
with tempfile.TemporaryDirectory() as workdir:
temp_dir = Path(workdir)
print(temp_dir)
sub_odk_extract = urllib.request.urlretrieve(sub_odk_url, 'sub_odk.xlsx')
sub_odk = sub_odk_extract[0]
sub_odk_path = temp_dir / sub_odk
print('test: ', sub_odk_path, ' Path', isfile(sub_odk_path.read()))
the following `print(temp_dir)` is printing the path as:
> C:\Users\myUser\AppData\Local\Temp\tmp3efhg0aw
Also when we check if `sub_odk` is a file that was successfully extracted the returned value is true. Also we were able to test it out of the temporary folder successfully.
However when we move it to the temp folder as:
sub_odk_path = temp_dir / sub_odk
The sub_odk_path is not considered as a file as the result of the print is:
> test: C:\Users\myUser\AppData\Local\Temp\tmp3efhg0aw\sub_odk.xlsx
> Path False
How can we move any file into a temporary folder and do changes over it before the deletion of the temp folder.
如果您有任何其他问题,请随时提出。
英文:
We've created a temporary directory within a python function to move files into for processing:
with tempfile.TemporaryDirectory() as workdir:
temp_dir = Path(workdir)
print(temp_dir)
sub_odk_extract = urllib.request.urlretrieve(sub_odk_url, 'sub_odk.xlsx')
sub_odk = sub_odk_extract[0]
sub_odk_path = temp_dir / sub_odk
print('test: ', sub_odk_path, ' Path', isfile(sub_odk_path.read()))
the following print(temp_dir)
is printing the path as:
> C:\Users\myUser\AppData\Local\Temp\tmp3efhg0aw
Also when we check if sub_odk
is a file that was successfully extracted the returned value is true. Also we were able to test it out of the temporary folder successfully.
However when we move it to the temp folder as:
sub_odk_path = temp_dir / sub_odk
The sub_odk_path is not considered as a file as the result of the print is:
> test: C:\Users\myUser\AppData\Local\Temp\tmp3efhg0aw\sub_odk.xlsx
> Path False
How can we move any file into a temporary folder and do changes over it before the deletion of the temp folder.
答案1
得分: 1
在最后一个打印函数中,不太清楚您正在使用isfile()
函数调用什么函数。另外,您没有传递文件给它,而是传递了sub_odk_path.read()
的返回值,这很可能不是一个文件。
由于您正在使用pathlib,您可以直接使用Path.is_file()方法。
print('test: ', sub_odk_path, ' Path', sub_odk_path.is_file())
英文:
In that last print function, it's not entirely clear what function you're calling with isfile()
. Additionally, you're not passing a file to it, you're passing the return value of sub_odk_path.read()
, which is most likely not going to be a file.
Since you're using pathlib, you can just use the Path.is_file() method.
print('test: ', sub_odk_path, ' Path', sub_odk_path.is_file())
答案2
得分: 0
你试过使用 os.path.isfile(sub_odk_path)
而不是 isfile(...) 吗?
英文:
Have you tried os.path.isfile(sub_odk_path)
instead of isfile(...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论