Python Forum
Read and save file in chucksize
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read and save file in chucksize
#4
This part is useless:
existe_txt = os.path.exists(txt)
existe_xls = os.path.exists(xls)
The cause is, even if the files both are existing,
they could disappear after the check. This is not
a protection against Exception like FileNotFoundError
or PermissionError.

Python has for everything Exceptions.

def main():
    try:
        data = pd.read_csv(txt, sep="|", header=None, encoding='ISO-8859-1', chunksize=big_file)
        writer = pd.ExcelWriter('output.xlsx')
    except (FileNotFoundError, PermissionError) as err:
        print(err)
        return
    for df in data:
        ...

    ...


    writer.save()
You could catch more general Exception.
If you get for example a PermissionError and want to look up the inheritance:
PermissionError.mro()
Output:
[PermissionError, OSError, Exception, BaseException, object]
This Exception could be catched by: PermissionError, OSError, Exception
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Read and save file in chucksize - by zinho - Mar-24-2020, 06:08 PM
RE: Read and save file in chucksize - by zinho - Mar-27-2020, 02:31 PM
RE: Read and save file in chucksize - by zinho - Apr-01-2020, 06:56 PM
RE: Read and save file in chucksize - by DeaD_EyE - Apr-02-2020, 08:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 254 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Open/save file on Android frohr 0 362 Jan-24-2024, 06:28 PM
Last Post: frohr
  Recommended way to read/create PDF file? Winfried 3 2,959 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,585 Nov-09-2023, 10:56 AM
Last Post: mg24
  how to save to multiple locations during save cubangt 1 592 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,284 Jul-07-2023, 05:44 PM
Last Post: flash77
  read file txt on my pc to telegram bot api Tupa 0 1,166 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,162 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,397 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 7,133 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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