Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: deleting numbers in a file

Tags:

python

file

I need to delete numbers from a text file on windows XP. I am new to python and just installed it for data scrubbing.

I have stored the test file in C:\folder1\test1.txt

The contexts on test1.txt is just 1 line:

This must not b3 delet3d, but the number at the end yes 134411

I want to created a file result1.txt which contains

This must not b3 delet3d, but the number at the end yes

Here is what I tried so far

import os 

fin = os.open('C:\folder1\test1.txt','r')

I get the following error:

TypeError: an integer is required.

I am not sure what integer it is expecting.

Can you please let me know how to go about programming to get the result I want. Thanks a lot for your help.

like image 206
Zenvega Avatar asked Dec 27 '22 14:12

Zenvega


2 Answers

You're using open() in the os module, which takes a numeric file mode. You want instead the builtin open() function. Also, backslashes in strings take on a special meaning in Python; you need to double them up if you really mean backslashes. Try:

fin = open('C:\\folder1\\test1.txt','r')
like image 55
Russell Borogove Avatar answered Dec 30 '22 05:12

Russell Borogove


according to http://docs.python.org/library/os.html#file-descriptor-operations, os.open is looking for 'flag' parameter, made of one or more of these flags which 'r' is not. It also seems to indicate that you probably want to look into using open() rather than os.open()

like image 31
StevenV Avatar answered Dec 30 '22 04:12

StevenV