file-writing

How to append text to an existing text file

六眼飞鱼酱① 提交于 2019-12-11 05:28:58
问题 I have a program, which writes data to a text file in the following format. test1 - test1 - test1 - test1 After writing the first line, the text fields are cleared to make space for another round of user input. Simply said, this is how it should look: test1 - test1 - test1 - test1 test2 - test2 - test2 - test2 test3 - test3 - test3 - test3 Here's my code If Not File.Exists(path) Then MessageBox.Show("File doesn't exist in the given path", "No File", MessageBoxButtons.OK, MessageBoxIcon.Error)

Writing list of basic variables to text file

混江龙づ霸主 提交于 2019-12-11 05:13:18
问题 I want to write a text file that has some lines in the following format: result: variable1 +/- error1 result: variable2 +/- error2 And so on... So far I have: f = open('file_{a}.txt'.format(a=some_name), 'w') for i in range(len(variable)): f.write('result: ', variable[i], '+/-', error[i], '\n') Variable and error are floats, and some_name is a string. But I'm getting an error: TypeError: expected a string or other character buffer object I guess I need to format the f.write line differently

How to write data into .txt file in codeigniter

帅比萌擦擦* 提交于 2019-12-11 03:35:43
问题 I have a folder in assets name is login.My doubt is what how to set the path. $data=$id.'~'.$expense_type.'~'.$amount.'~'.$exp_date.'<br>'; $todate= date('Y-m-d'); echo $todate; if ( ! write_file('.../assets/login/log_'.$todate.'.txt', $data)) { echo 'Unable to write the file'; } else { echo 'File written!'; } No problem about the data but the path is wrong i set my path is 'localhost/project/pro/application/assets/login/(specific .txt file)' Output is Unable to write the file 回答1: Use

Writing arrays to a csv in columns

心已入冬 提交于 2019-12-10 23:13:54
问题 I have been trying to solve a basic problem (and have been Python 2.7, and have got no where. I have a csv file with column headings such as: a,b,c 1,2,3 1,2,3 1,2,3 This is saved as test1.csv I have managed to take each column and pass them into an array, so each column heading is at the start, followed by the data. How would I then write this back into a new csv file with the same order and structure? import csv f = open('test1.csv','rb') reader = csv.reader(f) headers = reader.next() print

How do I read and append to a text file in one pass?

北慕城南 提交于 2019-12-10 18:37:42
问题 I want to check if a string is inside a text file and then append that string if it's not there. I know I can probably do that by creating two separate with methods, one for reading and another for appending, but is it possible to read and append inside the same with method? The closest I came up with is this: with open("file.txt","r+") as file: content=file.read() print("aaa" in content) file.seek(len(content)) file.write("\nccccc") My file.txt: aaaaa bbbbb When I run the code for the first

Python: Writing int to a binary file

筅森魡賤 提交于 2019-12-10 17:14:09
问题 I have a program which calculates the offset(difference) and then stores them in an 16 bit unsigned int using numPy and I want to store this int into a binary file as it is in binary form. i.e. if the value of offset is 05, I want the file to show "01010000 00000000", but not as a string. The code I have written is: target = open(file_cp, 'wb') target.write('Entries') target.write('\n') Start = f.tell() while(!EOF): f.read(lines) Current = f.tell() offset = np.uint16(Current-Start) target

Autoit - Get the file size of all files in path

為{幸葍}努か 提交于 2019-12-08 13:52:53
问题 Im using a script to get all files and dirs in a specific and generates a .txt file of the result but i need to add the size of each file using the FileGetSize + ByteSuffix functions #include <File.au3> #include <WinAPIFiles.au3> Global Enum Step *2 $GETFILES_NOT_DIRECTORY, $GETFILES_NOT_EXISTS ; GetFiles @error Func G3tAllF1lesAndDirs($cpath, $txtname) Local $sFileList = '' GetFiles($sFileList, $cpath) ; This uses no global variable $sFileList = StringReplace($sFileList, '|', @CRLF) ;

OutOfMemory exception thrown while writing large text file

最后都变了- 提交于 2019-12-08 10:02:36
问题 I want to generate a string and then write it in to a .txt file. The problem is I get OutOfMemory exceptions when I attempt to do so. The file is large (about 10000 lines). I use String.Format and loops to create the string. How can I write this to a .txt file? string Text= @"..."; const string channelScalar = @"..."; Text= string.Format(...); foreach (Channel channel in ...) { switch (channel.Type) { case "...": Text= string.Format(Text, ChannelFrames(channel, string.Format(...); break; } }

Python: write and read blocks of binary data to a file

我只是一个虾纸丫 提交于 2019-12-06 09:26:49
I am working on a script where it will breakdown another python script into blocks and using pycrypto to encrypt the blocks (all of this i have successfully done so far), now i am storing the encrypted blocks to a file so that the decrypter can read it and execute each block. The final result of the encryption is a list of binary outputs (something like blocks=[b'\xa1\r\xa594\x92z\xf8\x16\xaa',b'xfbI\xfdqx|\xcd\xdb\x1b\xb3',etc...] ). When writing the output to a file, they all end up into one giant line, so that when reading the file, all the bytes come back in one giant line, instead of each

How to remove \n and \r from a string

核能气质少年 提交于 2019-12-05 13:30:58
问题 I currently am trying to get the code from this website: http://netherkingdom.netai.net/pycake.html Then I have a python script parse out all code in html div tags, and finally write the text from between the div tags to a file. The problem is it adds a bunch of \r and \n to the file. How can I either avoid this or remove the \r and \n. Here is my code: import urllib.request from html.parser import HTMLParser import re page = urllib.request.urlopen('http://netherkingdom.netai.net/pycake.html'