file-io

Write newline to csv in Python

本小妞迷上赌 提交于 2020-06-25 09:01:21
问题 I want to end each interation of a for loop with writing a new line of content (including newline) to a csv file. I have this: # Set up an output csv file with column headers with open('outfile.csv','w') as f: f.write("title; post") f.write("\n") This does not appear to write an actual \n (newline) the file. Further: # Concatenate into a row to write to the output csv file csv_line = topic_title + ";" + thread_post with open('outfile.csv','w') as outfile: outfile.write(csv_line + "\n") This,

Can't open file: “NameError: name <filename> is not defined”

元气小坏坏 提交于 2020-06-18 11:27:17
问题 I am creating a program to read a FASTA file and split at some specifc characters such as ' > ' etc. But I'm facing a problem. The program part is: >>> def read_FASTA_strings(seq_fasta): ... with open(seq_fasta.txt) as file: ... return file.read().split('>') The error: Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'seq_fasta' is not defined How to get rid of this problem? 回答1: You need to specify the file name as a string literal: open('seq_fasta.txt')

How to read and write an object to a text file in java?

时光怂恿深爱的人放手 提交于 2020-05-27 05:19:27
问题 I have an array of objects and I want to write them in a text file. So that I can later read the objects back in an array. How should I do it? Using Serialization. Deserialization is not working: public static void readdata(){ ObjectInputStream input = null; try { input = new ObjectInputStream(new FileInputStream("myfile.txt")); // getting end of file exception here } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } try { array =

Illegal Characters in Path when extracting zip file with C#

一个人想着一个人 提交于 2020-05-15 08:54:06
问题 I'm trying to write a method that extracts a zip file to a directory, finds a file in the extracted contents, reads the text in that file to a string, and returns that string. Here is my attempt private string _getDataFile(string zipFile) { string pathToFolder = @"C:\Path\To\The\File"; foreach (char c in Path.GetInvalidPathChars()) { pathToFolder = Regex.Replace(pathToFolder, c.ToString(), ""); } string pathToFile = pathToFolder + @"\model.dat"; ZipFile.ExtractToDirectory(zipFile,

C++: Using ifstream with getline();

女生的网名这么多〃 提交于 2020-05-09 19:17:25
问题 Check this program ifstream filein("Hey.txt"); filein.getline(line,99); cout<<line<<endl; filein.getline(line,99); cout<<line<<endl; filein.close(); The file Hey.txt has alot of characters in it. Well over a 1000 But my question is Why in the second time i try to print line. It doesnt get print? 回答1: According to the C++ reference (here) getline sets the ios::fail when count-1 characters have been extracted. You would have to call filein.clear(); in between the getline() calls. 回答2: The

Best way to copy or move files with Objective-C?

依然范特西╮ 提交于 2020-04-29 05:29:19
问题 I was wondering, is there a best practice to write an OSX programm that copies or moves a file from one place to another? is there some NSSomething method I can call? Do I have to work with Input/Output streams? Or is the best way maybe to just rely on passing commands to the finder? Bonus question: How do I get percentages a la "copy 12% complete" with one of these methods? Thanks for your help! 回答1: NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you

Best way to copy or move files with Objective-C?

纵饮孤独 提交于 2020-04-29 05:27:08
问题 I was wondering, is there a best practice to write an OSX programm that copies or moves a file from one place to another? is there some NSSomething method I can call? Do I have to work with Input/Output streams? Or is the best way maybe to just rely on passing commands to the finder? Bonus question: How do I get percentages a la "copy 12% complete" with one of these methods? Thanks for your help! 回答1: NSFileManager and NSWorkspace both have methods to move, copy, and delete files. Usually you

Getting the file used by a FileInputStream [duplicate]

半城伤御伤魂 提交于 2020-04-16 02:06:09
问题 This question already has answers here : Get file name from FileOutputStream (5 answers) Closed 3 years ago . Is it possible to obtain the File being used by a FileInputStream ? FileInputStream does not appear to have any methods for retrieving it. 回答1: There are no direct methods in FileInputStream API, but if you really wanted, you can use java reflection API to get the path (actual file name with full path) as shown below: FileInputStream fis = new FileInputStream(inputFile); Field field =

How to check if a file has content or not using C?

北城以北 提交于 2020-04-08 10:40:48
问题 I have a source file file1 and a destination file file2 , here I have to move content from file1 to file2 . So I have to do some validation first. I must check source file is existing or not? I can check using this: fp = fopen( argv[1],"r" ); if ( fp == NULL ) { printf( "Could not open source file\n" ); exit(1); } Then I have to check if the source file has any content or not? If it is empty, I have to throw some error message. This is what I've tried until the moment. 回答1: C version: if

Xamarin Android file system paths

痞子三分冷 提交于 2020-04-08 10:19:06
问题 I'm trying to write and read a file on the Android file system. A task you would think would be straight forward, but I just can't seem to find the right enumerator for a folder/path that actually exists. I have read several posts on the same subject but none of them give a straight answer to this question and those that seem to have fixed their issues, has done so with paths that doesn't seem to work for me. Some suggestions seem to be related to System.IO while others seem to be of the Java