0

I have a code like this to process all files in folder and I've got a problem with last line. I want to save file after each iteration with custom name. Expample:

File in folder = SX.txt

Input value = 2

New file name = SX_2.txt

import glob
import pandas as pd
import os
    
txtfiles = []
for file in glob.glob("*.txt"):
    txtfiles.append(file)
print(txtfiles)

name_5=input("Mode number to extract:")

        
for filepath in glob.glob('*.txt'):
    with open(filepath) as file:
        #data1 = pd.DataFrame(pd.np.empty((0, 5))) 
        data1 = pd.read_csv(file, delim_whitespace=True , index_col=None, header=None)
        data1.columns  = ["a", "b", "c", "d","e"]

        data1['mode'] = data1.groupby(['b']).ngroup()
        data1['mode_1'] = data1['mode']+1
        data_d = data1[data1['mode_1'] == float(name_5)]
        del data_d["mode"]
        data_d["b"]=data_d["b"].round(decimals=3)
        data_d["c"]=data_d["c"].round(decimals=3)

        print(data_d)

        data_d.to_csv(f'{file} + "_" + {name_5}.csv', sep=' ', index=False)

Last line gives an error:

OSError: [Errno 22] Invalid argument: '<_io.TextIOWrapper name=\'SX1.txt\' mode=\'r\' encoding=\'cp1252\'> + "_" + 2.csv'

Could you help please? Thanks

1
  • and what is the problem ? Commented Jan 29, 2022 at 16:26

1 Answer 1

2

Problem

data_d.to_csv(f'{file} + "_" + {name_5}.csv', sep=' ', index=False)
  • You have confused what file is, that is a file descriptor openned on the original file,
  • you use a + in a f-string, that would string like 'SX + "_" + 5.csv'
  • if you want to keep extension, don't write .csv

Fix

data_d.to_csv(f'{filename[:-4]}_{name_5}.txt', sep=' ', index=False)

To simplify your comprehension : use file descriptor for both read and write OR none (pandas handles filename string)

txtfiles = glob.glob("*.txt")
print(txtfiles)
name_5 = input("Mode number to extract:")
for filename in txtfiles:
    data1 = pd.read_csv(filename, delim_whitespace=True, index_col=None, header=None)
    data1.columns = ["a", "b", "c", "d", "e"]
    data_d = data1[data1['mode_1'] == float(name_5)]
    # ...
    data_d.to_csv(f'{filename[:-4]}_{name_5}.csv', sep=' ', index=False)
Sign up to request clarification or add additional context in comments.

1 Comment

Great, thank a lot!!! Now it's working! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.