Python Forum
Manipulating files Python 2.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating files Python 2.7
#1
Folks,

I have a difficulty here to manipulate files.

Objective: I'm developing a script that needs to create folders and copy files from a source, the script reads the user the number of months to create. The script replicates the files equal the number of months to be created, if in 2017 need to change a string to a .txt file into the directory.

original string within the file: ANO INICIO DO ESTUDO 2016
String is to be changed: ANO INICIO DO ESTUDO 2017

PS: The file to be changed is not the original but a copy

It is possible to change a line from a txt file?

What I need:

Read program source file and play to an array
identifying a portion of a string within the array
modify the string if it is found to be part of the string
delete the source file and write another with the same name, or simply change a string (a word in a row) within the source file.
FILENAME_NEWAVE = Path of the source file
STRING_DGER = String to be searched
FILE_DATE = Year

This is not working, you are writing in the source file
def find_word_in_file_dger(FILENAME_NEWAVE, STRING_DGER, FILE_DATE):
   f = open(FILENAME_NEWAVE, "r+")
   file_array = f.readlines()
   for i in file_array:
       if i.find(STRING_DGER.encode('utf-8')):
           f.write(i)
       else:
           print ("TO LENDO O ARRAY")
           if FILE_DATE == "2016":
               continue
           else:
               i.replace(STRING_DGER, "ANO INICIO DO ESTUDO " + FILE_DATE)
               f.write(i)
               print("TO ESCREVENDO A LINHA CORRETAMENTE MLK!! ")
           return i
   f.close()
   return False

Correct script
Edit:
I have fixed it,mark all code next time an push "Remove Formatting" button.
Then "Insert Python tag" button.
Reply


Messages In This Thread
Manipulating files Python 2.7 - by hugobaur - Oct-21-2016, 01:20 PM
RE: Manipulating files Pytohn 2.7 - by Ofnuts - Oct-21-2016, 04:53 PM
RE: Manipulating files Pytohn 2.7 - by hugobaur - Oct-25-2016, 11:53 AM
RE: Manipulating files Python 2.7 - by Ofnuts - Oct-25-2016, 01:02 PM
RE: Manipulating files Python 2.7 - by hugobaur - Oct-31-2016, 06:37 PM
RE: Manipulating files Python 2.7 - by snippsat - Oct-31-2016, 07:16 PM
RE: Manipulating files Python 2.7 - by hugobaur - Nov-01-2016, 12:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Manipulating data from a CSV EvanS1 5 2,767 Jun-12-2020, 05:59 PM
Last Post: perfringo
  manipulating two lists rancans 8 3,285 Apr-16-2020, 06:00 PM
Last Post: deanhystad
  Manipulating index value, what is wrong with this code? Emun 1 1,798 Feb-05-2020, 07:18 AM
Last Post: perfringo
  Manipulating the filename of an output script mckinneycm 4 11,946 Jan-15-2020, 07:29 PM
Last Post: mckinneycm
  Manipulating Excel with Python. Spacely 2 3,677 Jun-25-2019, 01:57 AM
Last Post: Dequanharrison
  Manipulating CSV Prince_Bhatia 1 1,980 Apr-25-2019, 11:55 AM
Last Post: Gribouillis
  Reading and manipulating csv Prince_Bhatia 11 5,147 Mar-14-2019, 11:40 AM
Last Post: Larz60+
  Manipulating an Excel Workbook Stanimal 4 3,029 Jan-18-2019, 11:03 PM
Last Post: Stanimal
  Running a python tool transforming xml files into epub files silfer 7 5,481 May-10-2018, 03:49 PM
Last Post: snippsat
  Manipulating Binary Data arsenal88 10 8,741 Apr-25-2017, 02:30 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020