0

I need to convert my files into ascii and there are approximately 1000 files and it is really time-consuming if I do it one by one.

I'm new to python and what I want to do is to import all the files from a folder into an array so that I can convert them all at once.

Harshil Shah
  • 527
  • 4
  • 15
  • "Import all the files" is a bit vague, are your files txt files, and you want to store their text into an array? If yes, consider using os.listdir(), open() and read() to obtain those texts. – NyuB Oct 12 '18 at 13:36
  • No, they are in netCDF format and I want to convert them to ascii and read the values. – Harshil Shah Oct 12 '18 at 13:37
  • To read directly binaries values from a file, you can use open("filename",'rb') And then use the read(n) function, wich read n bits from the file. – NyuB Oct 12 '18 at 13:44
  • Yes I have tried that and it works fine but I will have to change the path every time and I have approximately 1000 files so it's not efficient to do that. – Harshil Shah Oct 12 '18 at 13:46

1 Answers1

0

This should be somewhere close to what you are looking for.

import os
import binascii
from sys import platform

# This was retrieved from the answer available at https://stackoverflow.com/questions/7396849/convert-binary-to-ascii-and-vice-versa
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
    bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
    return bits.zfill(8 * ((len(bits) + 7) // 8))

def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
    n = int(bits, 2)
    return int2bytes(n).decode(encoding, errors)

def int2bytes(i):
    hex_string = '%x' % i
    n = len(hex_string)
    return binascii.unhexlify(hex_string.zfill(n + (n & 1)))


def convert_files(path):
    files = os.listdir(path)
    for el in files:
        with open(el, 'rb') as f:
            result = text_from_bits(f.read())
        if platform == "linux" or platform == "linux2":
            with open("{}/{}.converted".format(path, el), 'a+') as f:
                f.write(result)
        elif platform == "win32":
            with open("{}\{}.converted".format(path, el), 'a+') as f:
                f.write(result)
Ilhicas
  • 1,291
  • 1
  • 18
  • 24