我如何使用C#将项目资源中的文件复制到临时目录
我的项目资源文件夹中有install.bat文件,我要将其复制到c:\目录中
我的问题:如何在不将文件保存到磁盘的情况下打开文件(在文件的系统默认 [外部] 程序中)?
我的情况:我的资源中有文件,我想显示这些文件而不先将它们保存到磁盘。例如,我有一个xml
文件,我想在用户机器上用默认程序打开它来读取xml
文件,而不是先将它保存到磁盘。
我一直在做什么:到目前为止,我只是将文件保存到一个临时位置,但我无法知道他们何时不再需要该文件,所以我不知道何时/是否删除它。这是我的SSCCE代码(嗯,它主要是 sscce,除了资源......你必须自己创建):
package main;
import java.io.*;
public class SOQuestion {
public static void main(String[] args) throws IOException {
new SOQuestion().showTemplate();
}
/** Opens the temporary file */
private void showTemplate() throws IOException {
String tempDir = System.getProperty("java.io.tmpdir") + "\\BONotifier\\";
File parentFile = new File(tempDir);
if (!parentFile.exists()) {
parentFile.mkdirs();
}
File outputFile = new File(parentFile, "template.xml");
InputStream inputStream = getClass().getResourceAsStream("/resources/template.xml");
int size = 4096;
try (OutputStream out = …
Run Code Online (Sandbox Code Playgroud) 我已经经历了很多关于以编程方式清理临时文件和缓存的问题,而且我得到的答案是No its not possible
因为应用程序处于沙盒模式,我们无法访问其他应用程序的内容,除非我们有读取权限的情况.但最近我看到一个应用程序正在删除其他应用程序的临时文件.它是现场并由苹果批准.
下面是用户要求删除临时文件时提供的警报的屏幕截图.
所以我的问题是,删除临时文件和其他应用程序的缓存现在可以以编程方式进行,或者是否有任何特定的设备位置,我们可以从中清除所有应用程序的临时文件或缓存.我感到很困惑,我所看到的(清理应用程序)和我读到的它不可能(以前的堆栈溢出问题和博客).一些有价值的链接和评论表示赞赏.
我正在编写一个工具来验证 Python 中的 RPM 并打印出更改的文件中的实际差异。
我可以使用 .ftp 从 ftp 服务器获取 RPM ftplib
。我正在考虑保存 rpm/tmp
并使用subprocess
模块来运行
rpm2cpio myrpmfile.rpm | cpio -idmv
Run Code Online (Sandbox Code Playgroud)
获取 RPM 中的文件,以便我可以比较这些文件。之后,我将删除临时文件。
这似乎有点 hacky,那么这是最好的方法,还是有更优雅的 Python 解决方案?
一般来说,我在使用 Python3 的临时文件库时遇到问题。
我需要在临时目录中写入一个文件,并确保它在那里。我使用的第三方软件工具有时会失败,所以我不能直接打开文件,我需要先使用“while 循环”或其他方法验证它是否存在,然后再打开它。所以我需要搜索 tmp_dir (使用 os.listdir() 或等效的)。
在评论中将感谢特定的帮助/解决方案和一般帮助。
谢谢你。
小示例代码:
import os
import tempfile
with tempfile.TemporaryDirectory() as tmp_dir:
print('tmp dir name', tmp_dir)
# write file to tmp dir
fout = open(tmp_dir + 'file.txt', 'w')
fout.write('test write')
fout.close()
print('file.txt location', tmp_dir + 'lala.fasta')
# working with the file is fine
fin = open(tmp_dir + 'file.txt', 'U')
for line in fin:
print(line)
# but I cannot find the file in the tmp dir like I normally use os.listdir()
for file …
Run Code Online (Sandbox Code Playgroud) 我正在使用Symfony 2.7,并且我有一个处理excel文件的表单。当我尝试有意地创建表单错误(或当我捕获任何错误并创建表单错误时-我的意思是在表单中显示我的错误消息)在重新加载页面后(我认为是在那一刻),发生这种情况:
request.CRITICAL: Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: "The file "/tmp/phpFoVva0" does not exist" at .../vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php line 123 {"exception":"[object] (Symfony\\Component\\HttpFoundation\\File\\Exception\\FileNotFoundException(code: 0): The file \"/tmp/phpFoVva0\" does not exist at .../vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php:123)"} []
Run Code Online (Sandbox Code Playgroud)
表单代码通常会被处理,但是当它完成或以表单错误结束时,会弹出此异常。访问权限没有问题。错误的文件不是此过程中的excel临时文件。这是一些在后台处理的.tmp文件。
我注意到此错误仅在特定情况下弹出。在项目和页面中,我拥有具有“获取”(即我尝试使用该表单导入的内容)和“项目”的代理商。当我尝试将其导入没有项目的代理商中时,一切都很好,并且显示格式错误或成功。但是,当我将其导入具有至少一个Project的代理商中时,会弹出此异常。
所有表单都通过.yml config注册,并在Controller中启动,如下所示:
$importAcquisitionBudgetType = $this->get("app.form_excel.import_acquisitions_budget_type");
$importAcquisitionBudgetType->setAgency($agency);
$acquisitionsBudgetForm = $this->createForm($importAcquisitionBudgetType);
$acquisitionsBudgetForm->handleRequest($request);
Run Code Online (Sandbox Code Playgroud)
我没有其他选择,因此每条建议都非常重要。
非常感谢。
我创建了一个R Shiny应用程序,该程序每天使用批处理文件自动运行。启动应用程序时,一切正常,但第二天崩溃,我收到以下消息:
Warning in file(open = "w+") :
cannot open file
'C:\Users\bertin\AppData\Local\Temp\RtmpKiBPOU\Rf3f835d1a66' : No such file or directory
Warning: Error in file: cannot open the connection
[No stack trace available]
Run Code Online (Sandbox Code Playgroud)
实际上,此问题与tempdir()
由执行闪亮应用程序的R会话创建的文件夹有关。一定时间后,此文件夹将自动删除。每次刷新时都必须删除所有Temp文件吗?还是相反,是否需要防止R删除Temp文件夹上的所有闪亮的临时文件?谢谢!
编辑 -这是有意产生错误的方法:
tempdir()
dir.exists(tempdir())
library(shiny)
# Windows shell required
shinyApp(
ui = fluidPage("Please reload to see me fail."),
server = function(input, output) {
shell(paste("rmdir", dQuote(
normalizePath(tempdir(), winslash = "/", mustWork = FALSE), q = FALSE
), "/s /q"))
}
)
Run Code Online (Sandbox Code Playgroud)
我们在nginx中使用client_body_in_file_only选项,以允许通过Ajax上传文件.配置如下所示:
location ~ ^(\/path1|\path2)$ {
limit_except POST { deny all; }
client_body_temp_path /path/to/app/tmp;
client_body_in_file_only on;
client_body_buffer_size 128K;
client_max_body_size 1000M;
#this option is a quick hack to make sure files get saved on (ie this type of request goes to) on a specific server
proxy_pass http://admin;
proxy_pass_request_headers on;
proxy_set_header X-FILE $request_body_file;
proxy_set_body off;
proxy_redirect off;
# might not need?
proxy_read_timeout 3m;
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,但处理请求的Web服务器进程(Mongrel)必须具有进入sudo
的临时文件headers['X-FILE']
,然后才能对其执行任何操作.这是因为临时文件具有600
权限.
我对这种方法不满意,这要求我们编辑/etc/sudoers
文件以允许Web服务器用户在sudo chmod
没有密码的情况下进行操作.感觉非常不安全.
有没有办法,使用nginx配置,更改创建的临时文件的权限,例如775?
编辑:我只是尝试umask
在nginx init配置中更改选项的值,然后重新启动nginx,但它没有帮助.它一直在0022 …
我在创建新的 ipython 笔记本时遇到问题。以前我曾经这样做到 new -> python 但现在我看不到任何 python 选项。另外,我无法打开任何 ipython 笔记本,我已经上传了几个小时了。
我浏览了那里的例子,似乎没有找到合适的。
想要使用 Python 将内存中的文件解压缩到临时目录mkdtemp()
。
像这样的东西感觉很直观,但我找不到正确的语法:
import zipfile
import tempfile
zf = zipfile.Zipfile('incoming.zip')
with tempfile.mkdtemp() as tempdir:
zf.extractall(tempdir)
# do stuff on extracted files
Run Code Online (Sandbox Code Playgroud)
但这会导致:
AttributeError Traceback (most recent call last)
<ipython-input-5-af39c866a2ba> in <module>
1 zip_file = zipfile.ZipFile('incoming.zip')
2
----> 3 with tempfile.mkdtemp() as tempdir:
4 zip_file.extractall(tempdir)
AttributeError: __enter__
Run Code Online (Sandbox Code Playgroud)