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
524
votes
8 answers

Why does Node.js' fs.readFile() return a buffer instead of string?

I'm trying to read the content of test.txt(which is on the same folder of the Javascript source) and display it using this code: var fs = require("fs"); fs.readFile("test.txt", function (err, data) { if (err) throw err; …
Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
515
votes
1 answer

Automatically creating directories with file output

Say I want to make a file: filename = "/foo/bar/baz.txt" with open(filename, "w") as f: f.write("FOOBAR") This gives an IOError, since /foo/bar does not exist. What is the most pythonic way to generate those directories automatically? Is it…
Phil
  • 6,686
  • 2
  • 19
  • 25
511
votes
12 answers

UnicodeEncodeError: 'charmap' codec can't encode characters

I'm trying to scrape a website, but it gives me an error. I'm using the following code: import urllib.request from bs4 import BeautifulSoup get = urllib.request.urlopen("https://www.website.com/") html = get.read() soup = BeautifulSoup(html) And…
SstrykerR
  • 7,982
  • 3
  • 12
  • 11
455
votes
14 answers

Easiest way to read from and write to files

There are a lot of different ways to read and write files (text files, not binary) in C#. I just need something that is easy and uses the least amount of code, because I am going to be working with files a lot in my project. I only need something…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
452
votes
13 answers

Reading binary file and looping over each byte

In Python, how do I read in a binary file and loop over each byte of that file?
Jesse Vogt
  • 16,229
  • 16
  • 59
  • 72
442
votes
13 answers

Write lines of text to a file in R

In the R scripting language, how do I write lines of text, e.g., the following two lines Hello World to a file named "output.txt"?
amarillion
  • 24,487
  • 15
  • 68
  • 80
436
votes
26 answers

Delete directories recursively in Java

Is there a way to delete entire directories recursively in Java? In the normal case it is possible to delete an empty directory. However when it comes to deleting entire directories with contents, it is not that simple anymore. How do you delete…
paweloque
  • 18,466
  • 26
  • 80
  • 136
414
votes
18 answers

How to create a temporary directory/folder in Java?

Is there a standard and reliable way of creating a temporary directory inside a Java application? There's an entry in Java's issue database, which has a bit of code in the comments, but I wonder if there is a standard solution to be found in one of…
Peter Becker
  • 8,795
  • 7
  • 41
  • 64
414
votes
15 answers

Rename multiple files in a directory in Python

I'm trying to rename some files in a directory using Python. Say I have a file called CHEESE_CHEESE_TYPE.*** and want to remove CHEESE_ so my resulting filename would be CHEESE_TYPE I'm trying to use the os.path.split but it's not working properly.…
Jeff
  • 4,151
  • 3
  • 16
  • 4
413
votes
17 answers

How to read text file from classpath in Java?

I am trying to read a text file which is set in CLASSPATH system variable. Not a user variable. I am trying to get input stream to the file as below: Place the directory of file (D:\myDir) in CLASSPATH and try below: InputStream in =…
Chaitanya MSV
  • 6,706
  • 12
  • 40
  • 46
398
votes
4 answers

Does reading an entire file leave the file handle open?

If you read an entire file with content = open('Path/to/file', 'r').read() is the file handle left open until the script exits? Is there a more concise method to read a whole file?
tMC
  • 18,105
  • 14
  • 62
  • 98
390
votes
9 answers

What's the fastest way to read a text file line-by-line?

I want to read a text file line by line. I wanted to know if I'm doing it as efficiently as possible within the .NET C# scope of things. This is what I'm trying so far: var filestream = new System.IO.FileStream(textFilePath, …
Loren C Fortner
  • 4,217
  • 3
  • 18
  • 12
390
votes
16 answers

How to delete files/subfolders in a specific directory at the command prompt in Windows

Say, there is a variable called %pathtofolder%, as it makes it clear it is a full path of a folder. I want to delete every single file and subfolder in this directory, but not the directory itself. But, there might be an error like 'this file/folder…
Deniz Zoeteman
  • 9,691
  • 26
  • 70
  • 97
375
votes
12 answers

Lazy Method for Reading Big File in Python?

I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into another file and read next piece. Is there any method to yield these pieces…
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
371
votes
21 answers

Read and write a String from text file

I need to read and write data to/from a text file, but I haven't been able to figure out how. I found this sample code in the Swift's iBook, but I still don't know how to write or read data. import Cocoa class DataImporter { /* DataImporter…
Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77