我正在尝试从我的数据库中的条目创建一个临时文件。我做了一个print(filedata['data']),我可以看到数据正在被正确检索,但即使temp_h.flush()我从未看到任何数据保存到文件中。当我去检索它时,文件是空的。有人可以向我解释是什么导致了这种情况,如果有更好的方法来做我正在尝试用 PIL 做的事情,我也很想学习。我在带有 EXT3 文件系统的 Linux 系统上运行它
def main():
'''Main'''
for nohisto in thestash.stash.get_files():
fileid = str(nohisto['_id'])
filedata = openstash(fileid)
temp_h = tempfile.TemporaryFile(mode='r+')
temp_h.write(filedata['data'])
temp_h.flush()
time.sleep(3)
myhisto = Image.open(temp_h).histogram()
Run Code Online (Sandbox Code Playgroud) 我运行了一些这样的代码
(defun writeFile (name content)
(with-open-file (stream name
:direction :output
:if-exists :overwrite
:if-does-not-exist :create)
(format stream content)))
(writeFile "C:\Users\Peter\test.txt" "Test...")
Run Code Online (Sandbox Code Playgroud)
但后来我检查了我的C:\Users\Peter目录,它不包含创建一个名为test.txt. 我究竟做错了什么?
我有一个 python 脚本,它生成一个大文本文件,该文件需要一个特定的文件名,稍后将使用 FTPd。创建文件后,它会将其复制到新位置,同时修改日期以反映发送日期。唯一的问题是复制的文件缺少原始文件的最后几行。
from shutil import copy
// file 1 creation
copy("file1.txt", "backup_folder/file1_date.txt")
Run Code Online (Sandbox Code Playgroud)
这可能是什么原因造成的?原始文件是否未完成写入导致副本只是获取那里的内容?
出于某种原因,我的回调OVERLAPPED在调用ReadFileEx. 什么会导致这种情况?
更新 - 示例:
#include <stdio.h>
#include <Windows.h>
void __stdcall completion_routine(
unsigned long dwErrorCode,
unsigned long dwNumberOfBytesTransfered,
OVERLAPPED *lpOverlapped)
{
printf("Overlapped = %p\n", lpOverlapped);
}
int _tmain(int argc, LPTSTR argv[])
{
HANDLE hvolume = CreateFile(
_T("C:\\Windows\\Notepad.exe"), FILE_READ_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, 0, NULL);
char tempbuf[512];
OVERLAPPED tempoverlapped = { };
printf("%p\n", &tempoverlapped);
if (ReadFileEx(hvolume, tempbuf, sizeof(tempbuf),
&tempoverlapped, &completion_routine)
&& GetLastError() == ERROR_SUCCESS)
{
SleepEx(INFINITE, TRUE);
}
}
Run Code Online (Sandbox Code Playgroud) 如果我这样做
fis = new FileInputStream(new File(".").getAbsolutePath() + "/sudoinput.txt");
Run Code Online (Sandbox Code Playgroud)
它试图写入服务器上的这个位置。我不确定这是否是一个可写的地方。
FILE NAME (fos)::::::::::::::::::/opt/tomcat/temp/./sudoinput.txt
FILE NAME (fis)::::::::::::::::::/opt/tomcat/temp/./sudoinput.txt
Run Code Online (Sandbox Code Playgroud)
我想写入 webapps/sudoku/WEB-INF/classes 基本上是 C:\Users...\git\sudo-project\sudo\src\main\resources
在 Eclipse Windows 7 上,如果我给出这个错误 src\main\resources\sudoinput.txt (The system cannot find the path specified)
fis = new FileInputStream("src/main/resources/sudoinput.txt");
Run Code Online (Sandbox Code Playgroud)
我也试过这个:
fis = new FileInputStream("src\\main\\resources\\sudoinput.txt");
Run Code Online (Sandbox Code Playgroud)
但不起作用。
我应该如何创建一个 fileinputstream 才能写入 src/main/resources ?请注意,我正在使用 eclipse windows 进行开发,如果这改变了需要指定路径的方式,我会将 .war 文件上传到 unix 服务器。
我有一个.ear,一个.jar和一个.war包。我正在通过检查文件系统和注册映射动态地读取我的 servlet 类。
问题是它适用于开发,我能够读取目录并且正确的文件名确实能够创建映射。问题是如果我打包我的耳朵是不行的:
File( ... ).isDirectory()
File( ... ).listFiles()
null一个有效的目录路径。我已经通过检测不以扩展名结尾的结束路径解决了目录检查问题(出于我的目的,这样的验证就足够了),但我仍然无法列出给定路径中的文件。
鉴于这种情况,我有一个问题:
如何在运行时从包中列出文件名.war?
当前代码:
private void addAllFiles( File dir, ArrayList<ConventionPath> list ) {
for ( File file : dir.listFiles() ) { // dir.listFiles() comes null here =/
String absolutePath = file.getAbsolutePath();
boolean isActionDir = ConventionFiles.isActionDir( absolutePath, fileSeparator );
boolean isActionFile = ConventionFiles.isActionFile( absolutePath );
if ( isActionDir ) {
addAllFiles( file, list );
}
if …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,它从一个文件中读取文本并将其复制到一个新文件中。使用 while 循环和 ReadFile/Writefile 函数,我的程序可以工作……但我的程序不会停止运行,除非我强制停止它。我猜我没有正确关闭我的句柄,或者我的 while 循环可能设置错误。一旦我强制停止我的程序,文件就会以新名称成功复制到新位置。
int n = 0;
while(n=ReadFile(hFileSource, buffer, 23, &dwBytesRead, NULL)){
WriteFile(hFileNew, buffer, dwBytesRead, &dwBytesWritten, NULL);
}
CloseHandle(hFileSource);
CloseHandle(hFileNew);
return 0;
Run Code Online (Sandbox Code Playgroud) 用块打开文件之后会关闭它吗?
File.open('test.txt') do |txt|
...
end
Run Code Online (Sandbox Code Playgroud)
我想知道在这段代码的末尾是否关闭了此文件,或者我是否仍然应该调用:
File.close()
Run Code Online (Sandbox Code Playgroud) 我正在做一个小家庭项目,以了解有关集合和 FileIO 的更多信息。
我制作了一个小赛车应用程序,用户在其中输入所有都是字符串的赛车手详细信息,并将这些详细信息插入到列表列表中,然后一旦用户单击“保存”按钮,列表的内容就会被提取为文本文件。我已经成功地能够写入文本文件,但是我现在想更改它,以便我能够将数据附加到文本文件而不是每次将数据写入文本文件时覆盖以前的数据,我是在使用 AppendAllText() 时遇到此问题。
以下是我的 People 类如何处理列表中的数据输入:
class People
{
public string Name { get; set; }
public string Car { get; set; }
public string Place { get; set; }
public List<string> myList = new List<string>();
public void ShowList()
{
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine("" + myList[i] + "\n|");
}
}
public void AddToList()
{
myList.Add("Name: " + Name + " Car: " + Car + " Place: " + Place); …Run Code Online (Sandbox Code Playgroud) 我想知道如何在 python 中加载几个 matlab 文件。我对python很陌生,知道的不多
我正在使用scipy.io.loadmat('/~/20130228_100000_3D_E=1.mat'),然后以 .vts 格式保存数据
如何通过只运行我的程序一次来使用我拥有的所有可用 matlab 文件?
谢谢
file-io ×10
file ×3
python ×3
java ×2
winapi ×2
c ×1
c# ×1
collections ×1
common-lisp ×1
jakarta-ee ×1
jboss ×1
lisp ×1
loops ×1
matlab ×1
maven ×1
python-3.x ×1
ruby ×1
visual-c++ ×1
war ×1
while-loop ×1