Questions tagged [file-io]

File I/O is input/output that involves the file system. This could include performing operations on directories and files, such as creation and deletion, reading files, and writing output to files.

For an introduction on File I/O in C, see C Programming / File I/O on Wikibooks.

21104 questions
934
votes
17 answers

open() in Python does not create a file if it doesn't exist

What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it as read/write? From what I read, file = open('myfile.dat', 'rw') should do this, right? It is not working for me (Python 2.6.2) and I'm…
trh178
  • 11,228
  • 5
  • 28
  • 37
926
votes
8 answers

How can I open multiple files using "with open" in Python?

I want to change a couple of files at one time, iff I can write to all of them. I'm wondering if I somehow can combine the multiple open calls with the with statement: try: with open('a', 'w') as a and open('b', 'w') as b: …
Frantischeck003
  • 9,271
  • 3
  • 15
  • 4
914
votes
26 answers

File to byte[] in Java

How do I convert a java.io.File to a byte[]?
Ben Noland
  • 34,230
  • 18
  • 50
  • 51
888
votes
8 answers

Print string to text file

I'm using Python to open a text document: text_file = open("Output.txt", "w") text_file.write("Purchase Amount: " 'TotalAmount') text_file.close() I want to substitute the value of a string variable TotalAmount into the text document. Can someone…
The Woo
  • 17,809
  • 26
  • 57
  • 71
804
votes
24 answers

How do I save a String to a text file using Java?

In Java, I have text from a text field in a String variable called "text". How can I save the contents of the "text" variable to a file?
Justin White
  • 8,273
  • 4
  • 19
  • 9
776
votes
8 answers

Read file line by line using ifstream in C++

The contents of file.txt are: 5 3 6 4 7 1 10 5 11 6 12 3 12 4 Where 5 3 is a coordinate pair. How do I process this data line by line in C++? I am able to get the first line, but how do I get the next line of the file? ifstream myfile; myfile.open…
dukevin
  • 22,384
  • 36
  • 82
  • 111
769
votes
31 answers

How to append text to an existing file in Java?

I need to append text repeatedly to an existing file in Java. How do I do that?
flyingfromchina
  • 9,571
  • 12
  • 35
  • 38
749
votes
30 answers

Read a file one line at a time in node.js?

I am trying to read a large file one line at a time. I found a question on Quora that dealt with the subject but I'm missing some connections to make the whole thing fit together. var Lazy=require("lazy"); new Lazy(process.stdin) .lines …
Alex C
  • 16,624
  • 18
  • 66
  • 98
746
votes
25 answers

Batch file to delete files older than N days

I am looking for a way to delete all files older than 7 days in a batch file. I've searched around the web, and found some examples with hundreds of lines of code, and others that required installing extra command line utilities to accomplish the…
Kibbee
  • 65,369
  • 27
  • 142
  • 182
718
votes
9 answers

Read whole ASCII file into C++ std::string

I need to read a whole file into memory and place it in a C++ std::string. If I were to read it into a char[], the answer would be very simple: std::ifstream t; int length; t.open("file.txt"); // open input file t.seekg(0, std::ios::end); //…
Escualo
  • 40,844
  • 23
  • 87
  • 135
658
votes
7 answers

How to write to file in Ruby?

I need to read the data out of database and then save it in a text file. How can I do that in Ruby? Is there any file management system in Ruby?
ohana
  • 6,601
  • 2
  • 17
  • 5
642
votes
10 answers

How do I get the directory from a file's full path?

What is the simplest way to get the directory that a file is in? I'm using this to set a working directory. string filename = @"C:\MyDirectory\MyFile.bat"; In this example, I should get "C:\MyDirectory".
Even Mien
  • 44,393
  • 43
  • 115
  • 119
559
votes
27 answers

Clearing using jQuery

Is it possible to clear an control value with jQuery? I've tried the following: $('#control').attr({ value: '' }); But it's not working.
sagar
558
votes
16 answers

Read .mat files in Python

Is it possible to read binary MATLAB .mat files in Python? I've seen that SciPy has alleged support for reading .mat files, but I'm unsuccessful with it. I installed SciPy version 0.7.0, and I can't find the loadmat() method.
Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
530
votes
23 answers

How to read a local text file in the browser?

I’m trying to implemennt a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working. function readTextFile() { var rawFile = new XMLHttpRequest(); …
Danny
  • 9,199
  • 16
  • 53
  • 75