#🔒 pycharm error

65 messages · Page 1 of 1 (latest)

late hemlock
#

what's wrong with this function:

def convert(config):
    folder = get_files(config)
    for file_name in folder:
        if file_name.endswith('.csv'):
            csv_file = os.path.join(folder, file_name)
            base = os.path.splitext(csv_file)[0]
            excel_file = f'{base}.xlsx'
            wb = Workbook()
            ws = wb.active
            delimiter = check_delimiter(csv_file)
            with open(csv_file, newline='', encoding='utf8') as data:
                if delimiter:
                    reader = csv.reader(data, delimiter=delimiter)
                    for row in reader:
                        ws.append(row)
            wb.save(excel_file)```
i'm getting this error in pycharm
```Unexpected type(s):(list[str], str)Possible type(s):(LiteralString, LiteralString)(str | PathLike[str], str | PathLike[str])(bytes | PathLike[bytes], bytes | PathLike[bytes])(LiteralString, LiteralString)(str | PathLike[str], str | PathLike[str])(bytes | PathLike[bytes], bytes | PathLike[bytes])```
lime socketBOT
#

@late hemlock

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

forest rapids
hollow holly
balmy cradle
#

a function that expects (path, path) was given (list[path], path) (after a while looking at the error)

late hemlock
#

this is the other function def get_files(config): inputfiles = glob.glob(os.path.join(config.input_dir[1], '*.xlsx')) return inputfiles

balmy cradle
#

therefore you have to functools.reduce(os.path.join, folder + [file_name])

#

that's my bet

late hemlock
#

i got no error when i do def convert(folder): for file_name in os.listdir(folder): if file_name.endswith('.csv'): csv_file = os.path.join(folder, file_name) base = os.path.splitext(csv_file)[0] excel_file = f'{base}.xlsx' wb = Workbook() ws = wb.active delimiter = check_delimiter(csv_file) with open(csv_file, newline='', encoding='utf8') as data: if delimiter: reader = csv.reader(data, delimiter=delimiter) for row in reader: ws.append(row) wb.save(excel_file)

balmy cradle
late hemlock
#

how can i fix it?

#

or do i have to leave it as a path?

balmy cradle
balmy cradle
balmy cradle
#

looking at what I said, makes no sense because it's not the case for reduce, what I would do is print folder and see what's in there

late hemlock
#

the folder contains file1, file2,...

balmy cradle
#

something like
os.path.join(config.input_dir[1], file_name) instead

late hemlock
#

ok and which way is better?

balmy cradle
#

the argument you're giving is diferent

#

in the first one you're passing some sort of config instance

#

in the second you're passing the path to the folder as an argument

#

use the one that's more convenient for you

#

in the second I would use filter instead of an if but, whatever, it's your code, not that it makes any diference

late hemlock
#

config = get_args() this is config which gets argparse from user. the user give folders names

#

i was doing it as path but then i have to give the folder name in main function

#

so i thought it's better to take the folder directly from user input without main

balmy cradle
late hemlock
#

when i call the function in main(), i have to give the folder name as parameter

balmy cradle
#

I don't quite get it, ¿can you send code?

late hemlock
#

i mean in main function convert('my folder')

#

or the other way ```
config = get_args()
convert(config)

balmy cradle
# late hemlock i mean in main function ``` convert('my folder')```

yeah, my point is do can do either, I would go for the first one because convert is only cares about the folder, so imagine you want to call convert on another folder that is not the one that config especifies?
both cases are correct, just to your preference, if you only want a 200 lines script to do a super-especific task you don't need to worry aboutr that either

late hemlock
balmy cradle
#

for me? yeah
convert(config) is asured to convert the folder config.input_dir[1], imagine you want to convert config.input_dir[0]

late hemlock
#

yeah

#

and it's better to use filter than if?

balmy cradle
#

easier to read for me, again, it's your preference

#
def convert(folder):
    for file_name in filter(lambda f: file_name.endswith('.csv'), os.listdir(folder)):
        csv_file = os.path.join(folder, file_name)
        base = os.path.splitext(csv_file)[0]
        excel_file = f'{base}.xlsx'
        wb = Workbook()
        ws = wb.active
        delimiter = check_delimiter(csv_file)
        with open(csv_file, newline='', encoding='utf8') as data:
            if delimiter:
                reader = csv.reader(data, delimiter=delimiter)
                for row in reader:
                    ws.append(row)
        wb.save(excel_file)
late hemlock
#

alright

#

can u also tell me if this is good def check_delimiter(file): with open(file, newline='', encoding='utf8') as file: sample = file.readline() if ';' in sample: return ';' elif ',' in sample: return ',' else: return None

#

i wanted to check if the csv file has ; or , or nothing

balmy cradle
#

sure

late hemlock
#

ok

late hemlock
balmy cradle
late hemlock
#

how

#

the user has to change it

#

manually

balmy cradle
#

wasnt it passed as a parameter?

#

don't you call convert(config.input_dir[1]) on your main function?

late hemlock
#

no like this convert('my folder')

balmy cradle
late hemlock
balmy cradle
#

that's what I would do

late hemlock
#

ah ok

#

thank you

lime socketBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.