Questions tagged [file-handling]

file-handling is an abstraction of common actions such as creating, opening, closing, reading, updating, writing, comparing and deleting files

2915 questions
1433
votes
11 answers

How do I move a file in Python?

How can I do the equivalent of mv in Python? mv "path/to/current/file.foo" "path/to/new/destination/for/file.foo"
David542
  • 104,438
  • 178
  • 489
  • 842
319
votes
12 answers

How to check if a file is empty in Bash?

I have a file called diff.txt. I Want to check whether it is empty. I wrote a bash script something like below, but I couldn't get it work. if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm…
Mich
  • 3,253
  • 2
  • 14
  • 6
228
votes
8 answers

How can I download a file from a URL and save it in Rails?

I have a URL to an image which i want to save locally, so that I can use Paperclip to produce a thumbnail for my application. What's the best way to download and save the image? (I looked into ruby file handling but did not come across anything.)
Alok Swain
  • 6,409
  • 5
  • 36
  • 57
185
votes
7 answers

How to find and replace text in a file

My code so far StreamReader reading = File.OpenText("test.txt"); string str; while ((str = reading.ReadLine())!=null) { if (str.Contains("some text")) { StreamWriter write = new StreamWriter("test.txt"); } } I know how…
Win Coder
  • 6,628
  • 11
  • 54
  • 81
121
votes
10 answers

Write text files without Byte Order Mark (BOM)?

I am trying to create a text file using VB.Net with UTF8 encoding, without BOM. Can anybody help me, how to do this? I can write file with UTF8 encoding but, how to remove Byte Order Mark from it? edit1: I have tried code like this; Dim utf8…
VJOY
  • 3,752
  • 12
  • 57
  • 90
104
votes
9 answers

How to read a file line by line or a whole text file at once?

I'm in a tutorial which introduces files (how to read from file and write to file) First of all, this is not a homework, this is just general help I'm seeking. I know how to read one word at a time, but I don't know how to read one line at a time,…
Mody
  • 1,267
  • 3
  • 10
  • 11
58
votes
3 answers

Why does my Python code print the extra characters "" when reading from a text file?

try: data=open('info.txt') for each_line in data: try: (role,line_spoken)=each_line.split(':',1) print(role,end='') print(' said: ',end='') print(line_spoken,end='') except…
user5234170
53
votes
2 answers

Exception Handling and Opening a File?

Is it possible to use exceptions with file opening as an alternative to using .is_open()? For example: ifstream input; try{ input.open("somefile.txt"); }catch(someException){ //Catch exception here } If so, what type is someException?
Moshe
  • 57,511
  • 78
  • 272
  • 425
51
votes
5 answers

What does an assignment expression evaluate to in Java?

I encountered a statement in Java while ((line = reader.readLine()) != null) { out.append(line); } How do assignment operations return a value in Java? The statement we are checking is line = reader.readLine() and we compare it with null. Since…
sunder
  • 968
  • 2
  • 11
  • 31
46
votes
6 answers

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

My code is for a script that looks at a folder and deletes images that are under a resolution of 1920x1080. The problem I am having is that when my code runs; import os from PIL import Image while True: img_dir = r"C:\Users\Harold\Google…
user2885647
  • 855
  • 2
  • 9
  • 10
35
votes
4 answers

Php create a file if not exists

I try to create files and write the contents dynamically. Below is my code. $sites = realpath(dirname(__FILE__)).'/'; $newfile = $sites.$filnme_epub.".js"; if (file_exists($newfile)) { $fh = fopen($newfile, 'a'); fwrite($fh, 'd'); } else { …
shyamkarthick
  • 499
  • 1
  • 4
  • 17
34
votes
3 answers

Why is my iOS app not showing up in other apps' "Open in" dialog?

I am trying to implement the registering process that allows my iOS app to show up in the "Open in" list of other applications (as described in Apple's Document Interaction Programming Topics). I want my app to be able to handle audio from any app…
user577537
32
votes
9 answers

How to read text file by particular line separator character?

Reading a text file using streamreader. using (StreamReader sr = new StreamReader(FileName, Encoding.Default)) { string line = sr.ReadLine(); } I want to force that line delimiter should be \n not \r. So how can i do that?
User13839404
  • 1,803
  • 12
  • 37
  • 46
30
votes
6 answers

How do I clear the whole contents of a file in C?

I have a file with some of user1's data. I want to use the same file for user2 by clearing the content of the file. My idea is that when a new user comes, data of the previous user should be clear and the same file should be ready for the new user.…
mujahid
  • 691
  • 2
  • 12
  • 21
28
votes
4 answers

Difference between System.getProperty("line.separator"); and "\n"?

While developing GUI with Java FX, I seem to get different results with System.getProperty("line.separator"); and "\n" during writing to a file or getting data from internet. What basically is the difference?
Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
1
2 3
99 100