如何创建新文件? [英] How to create new files?

查看:77
本文介绍了如何创建新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试创建一个Python等价的C ++ifstream。上课,

,行为略有改变。


基本上,我希望有一个文件流。允许您使用'<<'<<'&'和'>>''运算符过载和流式处理数据的对象,

分别。到目前为止,这就是我所拥有的:


类文件流:

def __init __(self,filename):

self.m_file = open(filename," rwb")

#def __del __(self):

#self.m_file.close()


def __lshift __(自我,数据):

self.m_file.write(数据)

def __rshift __(self,data) :

self.m_file.read(数据)

到目前为止,我发现与fopen()的C ++版本不同,

Python''open()''调用在打开

时使用w模式不会为你创建文件。我得到一个例外,说该文件不存在
。我希望它会为我创建文件。有没有办法

make open()创建文件,如果它不存在,或者可能还有

另一个我可以用来创建文件的函数文件?我读了python文档,

我无法找到解决方案。


另外,你可能会注意到我的self.m_file。读()"函数是错误的,至少根据python docs
。 read()需要读取

字节的数量,但是我无法找到相当于G />
" sizeof()"的C ++。在Python中。如果我想从数据读入1字节,2字节或4

字节值到python我不知道怎么做。


非常感谢任何帮助。谢谢。

Hi,

I''m trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.

Basically, I want to have a "filestream" object that will allow you to
overload the ''<<'' and ''>>'' operators to stream out and stream in data,
respectively. So far this is what I have:

class filestream:
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )

# def __del__( self ):
# self.m_file.close()

def __lshift__( self, data ):
self.m_file.write( data )

def __rshift__( self, data ):
self.m_file.read( data )
So far, I''ve found that unlike with the C++ version of fopen(), the
Python ''open()'' call does not create the file for you when opened
using the mode ''w''. I get an exception saying that the file doesn''t
exist. I expected it would create the file for me. Is there a way to
make open() create the file if it doesn''t exist, or perhaps there''s
another function I can use to create the file? I read the python docs,
I wasn''t able to find a solution.

Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.

Any help is greatly appreciated. Thanks.

推荐答案

Robert Dailey写道:
Robert Dailey wrote:




我正在尝试创建一个Python等价的C ++ifstream。上课,

,行为略有改变。


基本上,我希望有一个文件流。允许您使用'<<'<<'&'和'>>''运算符过载和流式处理数据的对象,

分别。到目前为止,这就是我所拥有的:


类文件流:

def __init __(self,filename):

self.m_file = open(filename," rwb")

#def __del __(self):

#self.m_file.close()


def __lshift __(自我,数据):

self.m_file.write(数据)

def __rshift __(self,data) :

self.m_file.read(data)


到目前为止,我发现与fopen()的C ++版本不同,

Python''open()''调用在打开

时使用w模式不会为你创建文件。我得到一个例外,说该文件不存在
。我希望它会为我创建文件。有没有办法

make open()创建文件,如果它不存在,或者可能还有

另一个我可以用来创建文件的函数文件?我读了python文档,

我无法找到解决方案。


另外,你可能会注意到我的self.m_file。读()"函数是错误的,至少根据python docs
。 read()需要读取

字节的数量,但是我无法找到相当于G />
" sizeof()"的C ++。在Python中。如果我想从数据读入1字节,2字节或4

字节值到python我不知道怎么做。


非常感谢任何帮助。谢谢。
Hi,

I''m trying to create a Python equivalent of the C++ "ifstream" class,
with slight behavior changes.

Basically, I want to have a "filestream" object that will allow you to
overload the ''<<'' and ''>>'' operators to stream out and stream in data,
respectively. So far this is what I have:

class filestream:
def __init__( self, filename ):
self.m_file = open( filename, "rwb" )

# def __del__( self ):
# self.m_file.close()

def __lshift__( self, data ):
self.m_file.write( data )

def __rshift__( self, data ):
self.m_file.read( data )
So far, I''ve found that unlike with the C++ version of fopen(), the
Python ''open()'' call does not create the file for you when opened
using the mode ''w''. I get an exception saying that the file doesn''t
exist. I expected it would create the file for me. Is there a way to
make open() create the file if it doesn''t exist, or perhaps there''s
another function I can use to create the file? I read the python docs,
I wasn''t able to find a solution.

Also, you might notice that my "self.m_file.read()" function is wrong,
according to the python docs at least. read() takes the number of
bytes to read, however I was not able to find a C++ equivalent of
"sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
byte value from data into python I have no idea how I would do this.

Any help is greatly appreciated. Thanks.



open为我创建文件,所以我不确定你为什么认为它不适合你。


.read()方法接受字节数而不是缓冲区来存储字节

读取。


data = self.m_file.read( 4)将4个字节读入由

数据指向的字符串对象。


Python中的sizeof()是len()

-Larry

open creates files for me, so I''m uncertain why you think it isn''t for you.

the .read() method accepts the number of bytes not the buffer to store bytes
read.

data=self.m_file.read(4) would read 4 bytes into string object pointed to by
data.

sizeof() in Python is len()

-Larry


到目前为止,我发现与fopen()的C ++版本不同,
So far, I''ve found that unlike with the C++ version of fopen(), the

Python''open()''调用在打开时不会为你创建文件

使用'w'模式。我得到一个例外,说该文件不存在

Python ''open()'' call does not create the file for you when opened
using the mode ''w''. I get an exception saying that the file doesn''t
exist.



为我工作......


:〜

Works for me...

:~


mkdir foo

:〜
mkdir foo
:~


这篇关于如何创建新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆