file-io

UNIX: How to run a program with a file as an input

匆匆过客 提交于 2019-12-30 10:26:24
问题 I'm writing a bash script called 'run' that tests programs with pre-defined inputs. It takes in a file as the first parameter, then a program as a second parameter. The call would look like ./run text.txt ./check for example, the program 'run' would run 'check' with text.txt as the input. This will save me lots of testing time with my programs. right now I have $2 < text.txt > text.created So it takes the text.txt and redirects it as input in to the program specified, which is the second

Detecting that log file has been deleted or truncated on POSIX systems?

ぃ、小莉子 提交于 2019-12-30 09:53:41
问题 Suppose a long-running process writes to a log file. Suppose that log file is kept open indefinitely. Suppose that a careless system administrator deletes that log file. Can the program detect that this has happened? Is it safe to assume that fstat() will report a link count of zero for a deleted file? Truncation, it seems to me, is slightly trickier. In part, it depends on whether the file descriptor is running in O_APPEND mode. If the log file is not running with O_APPEND , then the current

seek(), then read(), then write() in python

主宰稳场 提交于 2019-12-30 08:31:21
问题 When running the following python code: >>> f = open(r"myfile.txt", "a+") >>> f.seek(-1,2) >>> f.read() 'a' >>> f.write('\n') I get the following (helpful) exception: Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 0] Error The same thing happens when openning with "r+". Is this supposed to fail? Why? Edit: Obviously, this is just an example, not what I am actually trying to execute. My actual goal was to verify that the files ends with "\n", or add one,

python how to write list of lists to file

三世轮回 提交于 2019-12-30 07:32:40
问题 I am trying to write a list of lists of strings to a file. My big list is of the forme: 1.0 '0:25.0' '1:50.0' '2:131.0' '3:202.0' 1.0 '0:2.0' '1:36.0' '2:131.0' '3:188.0' -1.0 '0:56.0' '1:53.0' '2:55.0' '3:58.0' -1.0 '0:50.0' '1:51.0' '2:48.0' '3:55.0' and so on ... the first column is of type int and the remaining are of type str. I have tried the following code: f = open('dataset', 'w') for i in range(len(mylist[0]): f.write(str(item) for item in mylist[i]) f.close() But I get the following

Split diary file into multiple files using Python

筅森魡賤 提交于 2019-12-30 07:21:56
问题 I keep a diary file of tech notes. Each entry is timestamped like so: # Monday 02012-05-07 at 01:45:20 PM This is a sample note Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat

How to send a file from a servlet the “right” way?

吃可爱长大的小学妹 提交于 2019-12-30 07:21:09
问题 I'm trying to send a file to a user from an http servlet. The servlet runs some identification tests (on the request) and then sends the client a file. This generally works but now I turned on my TOMCAT server redirects to https and when I try to access the servlet and download the file from either IE6 or IE8 it fails and I get this exception: java.lang.IllegalStateException: Cannot forward after response has been committed (on the localhost.log) and ClientAbortException: java.net

Insert line break when writing to file?

一曲冷凌霜 提交于 2019-12-30 07:05:25
问题 So My code looks like this: try { while ((line = br.readLine()) != null) { Matcher m = urlPattern.matcher (line); while (m.find()) { System.out.println(m.group(1)); //the println puts linebreak after each find String filename= "page.txt"; FileWriter fw = new FileWriter(filename,true); fw.write(m.group(1)); fw.close(); //the fw writes everything after each find with no line break } } I get right form of output at line System.out.println(m.group(1)); However when I later on want to write what

Read File line by line to variable and loop

坚强是说给别人听的谎言 提交于 2019-12-30 06:52:31
问题 I have a phone.txt like: 09236235965 09236238566 09238434444 09202645965 09236284567 09236235965 ..and so on.. How can I process this data line by line in C++ and add it to a variable. string phonenum; I know I have to open the file, but after doing so, what is done to access the next line of the file? ofstream myfile; myfile.open ("phone.txt"); and also about the variable, the process will be looped, it will make the phonenum variable the current line its processing from the phone.txt. Like

File Access Strategy in a Multi-Threaded Environment (Web App)

冷暖自知 提交于 2019-12-30 06:36:09
问题 I have a file which is an XML representation of some data that is taken from a Web service and cached locally within a Web Application. The idea being is that this data is very static, but just might change. So I have set it up to cache to a file, and stuck a monitor against it to check if it has been deleted. Once deleted, the file will be refreshed from its source and rebuilt. I am now running in to problems though, because obviously in a multi-threaded environment it falls over as it is

Checking a file existence on a remote SSH server using Python

三世轮回 提交于 2019-12-30 06:29:09
问题 I have two servers A and B. I'm suppose to send, let said an image file, from server A to another server B. But before server A could send the file over I would like to check if a similar file exist in server B. I try using os.path.exists() and it does not work. print os.path.exists('ubuntu@serverB.com:b.jpeg') The result return a false even I have put an exact file on server B. I'm not sure whether is it my syntax error or is there any better solution to this problem. Thank you 回答1: The os