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
3914
votes
20 answers

How do I tell if a file does not exist in Bash?

This checks if a file exists: #!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "File $FILE exists." else echo "File $FILE does not exist." fi How do I only check if the file does not exist?
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
3284
votes
17 answers

How can I delete a file or folder in Python?

How can I delete a file or folder in Python?
Zygimantas
  • 33,165
  • 6
  • 21
  • 23
3176
votes
30 answers

How do I find and restore a deleted file in a Git repository?

Say I'm in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I discover that I need to restore that file after deleting it. I know I can checkout a file using git checkout --…
avdgaag
  • 41,292
  • 7
  • 29
  • 26
2146
votes
47 answers

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

How can I create an Excel spreadsheet with C# without requiring Excel to be installed on the machine that's running the code?
mistrmark
  • 4,349
  • 6
  • 22
  • 15
1753
votes
35 answers

How do I create a Java string from the contents of a file?

I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited. Is there a better/different way to read a file into a string in Java? private String readFile(String file) throws…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1528
votes
35 answers

How do I create a file and write to it?

What's the simplest way to create and write to a (text) file in Java?
Drew Johnson
  • 18,973
  • 9
  • 32
  • 35
1521
votes
11 answers

How to redirect output to a file and stdout

In bash, calling foo would display any output from that command on the stdout. Calling foo > output would redirect any output from that command to the file specified (in this case 'output'). Is there a way to redirect output to a file and have it…
SCdF
  • 57,260
  • 24
  • 77
  • 113
1348
votes
17 answers

Correct way to write line to file?

How do I write a line to a file in modern Python? I heard that this is deprecated: print >>f, "hi there" Also, does "\n" work on all platforms, or should I use "\r\n" on Windows?
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
1048
votes
31 answers

Reading a plain text file in Java

It seems there are different ways to read and write data of files in Java. I want to read ASCII data from a file. What are the possible ways and their differences?
Tim the Enchanter
  • 10,837
  • 4
  • 21
  • 20
1041
votes
25 answers

Find all files in a directory with extension .txt in Python

How can I find all the files in a directory having the extension .txt in python?
usertest
  • 27,132
  • 30
  • 72
  • 94
1003
votes
14 answers

UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to

I'm trying to get a Python 3 program to do some manipulations with a text file filled with information. However, when trying to read the file I get the following error: Traceback (most recent call last): File "SCRIPT LOCATION", line NUMBER, in…
Eden Crow
  • 14,684
  • 11
  • 26
  • 24
998
votes
22 answers

How can I read a large text file line by line using Java?

I need to read a large text file of around 5-6 GB line by line using Java. How can I do this quickly?
manoj singh
  • 10,045
  • 3
  • 15
  • 3
976
votes
19 answers

How do I check if a file exists in Java?

How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl's -e $filename)? The only similar question on SO deals with writing the file and was thus answered using FileWriter which is obviously not…
DVK
  • 126,886
  • 32
  • 213
  • 327
970
votes
20 answers

Is there a way to check if a file is in use?

I'm writing a program in C# that needs to repeatedly access 1 image file. Most of the time it works, but if my computer's running fast, it will try to access the file before it's been saved back to the filesystem and throw an error: "File in use by…
Dawsy
  • 10,249
  • 5
  • 20
  • 13
954
votes
26 answers

Writing a list to a file with Python, with newlines

How do I write a list to a file? writelines() doesn't insert newline characters, so I need to do: f.writelines([f"{line}\n" for line in lines])
Josh Arenberg
  • 10,078
  • 5
  • 21
  • 17
1
2 3
99 100