csv to txt in python [duplicate]












0
















This question already has an answer here:




  • How do I copy a file in Python?

    14 answers




I have multiple .csv files in the fodler and I need to convert them to .txt files. Currently I am able just to rename them, but I want to leave the original files in place



import os,sys
folder = 'C:....'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.csv', '.txt')
output = os.rename(infilename, newname)









share|improve this question













marked as duplicate by Josh J, Ankit Jaiswal, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Do you just want to change the file-extension, or do you also want to change the content of the csv?

    – Niels Henkens
    Nov 15 '18 at 10:55











  • Copy file then rename the copy. stackoverflow.com/questions/123198/…

    – Josh J
    Nov 15 '18 at 10:57











  • What do you want to achieve? You are just making another copy with '.txt' extension.

    – Ankit Jaiswal
    Nov 15 '18 at 11:09
















0
















This question already has an answer here:




  • How do I copy a file in Python?

    14 answers




I have multiple .csv files in the fodler and I need to convert them to .txt files. Currently I am able just to rename them, but I want to leave the original files in place



import os,sys
folder = 'C:....'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.csv', '.txt')
output = os.rename(infilename, newname)









share|improve this question













marked as duplicate by Josh J, Ankit Jaiswal, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • Do you just want to change the file-extension, or do you also want to change the content of the csv?

    – Niels Henkens
    Nov 15 '18 at 10:55











  • Copy file then rename the copy. stackoverflow.com/questions/123198/…

    – Josh J
    Nov 15 '18 at 10:57











  • What do you want to achieve? You are just making another copy with '.txt' extension.

    – Ankit Jaiswal
    Nov 15 '18 at 11:09














0












0








0









This question already has an answer here:




  • How do I copy a file in Python?

    14 answers




I have multiple .csv files in the fodler and I need to convert them to .txt files. Currently I am able just to rename them, but I want to leave the original files in place



import os,sys
folder = 'C:....'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.csv', '.txt')
output = os.rename(infilename, newname)









share|improve this question















This question already has an answer here:




  • How do I copy a file in Python?

    14 answers




I have multiple .csv files in the fodler and I need to convert them to .txt files. Currently I am able just to rename them, but I want to leave the original files in place



import os,sys
folder = 'C:....'
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.csv', '.txt')
output = os.rename(infilename, newname)




This question already has an answer here:




  • How do I copy a file in Python?

    14 answers








python csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 10:53









Kamran AbbasovKamran Abbasov

246




246




marked as duplicate by Josh J, Ankit Jaiswal, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Josh J, Ankit Jaiswal, jpp python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Do you just want to change the file-extension, or do you also want to change the content of the csv?

    – Niels Henkens
    Nov 15 '18 at 10:55











  • Copy file then rename the copy. stackoverflow.com/questions/123198/…

    – Josh J
    Nov 15 '18 at 10:57











  • What do you want to achieve? You are just making another copy with '.txt' extension.

    – Ankit Jaiswal
    Nov 15 '18 at 11:09



















  • Do you just want to change the file-extension, or do you also want to change the content of the csv?

    – Niels Henkens
    Nov 15 '18 at 10:55











  • Copy file then rename the copy. stackoverflow.com/questions/123198/…

    – Josh J
    Nov 15 '18 at 10:57











  • What do you want to achieve? You are just making another copy with '.txt' extension.

    – Ankit Jaiswal
    Nov 15 '18 at 11:09

















Do you just want to change the file-extension, or do you also want to change the content of the csv?

– Niels Henkens
Nov 15 '18 at 10:55





Do you just want to change the file-extension, or do you also want to change the content of the csv?

– Niels Henkens
Nov 15 '18 at 10:55













Copy file then rename the copy. stackoverflow.com/questions/123198/…

– Josh J
Nov 15 '18 at 10:57





Copy file then rename the copy. stackoverflow.com/questions/123198/…

– Josh J
Nov 15 '18 at 10:57













What do you want to achieve? You are just making another copy with '.txt' extension.

– Ankit Jaiswal
Nov 15 '18 at 11:09





What do you want to achieve? You are just making another copy with '.txt' extension.

– Ankit Jaiswal
Nov 15 '18 at 11:09












1 Answer
1






active

oldest

votes


















0














You can use the copyfile of shutil.



import shutil

....< part of your code > ...
shutil.copyfile(filename, filename.replace('.csv', '.txt'))





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use the copyfile of shutil.



    import shutil

    ....< part of your code > ...
    shutil.copyfile(filename, filename.replace('.csv', '.txt'))





    share|improve this answer




























      0














      You can use the copyfile of shutil.



      import shutil

      ....< part of your code > ...
      shutil.copyfile(filename, filename.replace('.csv', '.txt'))





      share|improve this answer


























        0












        0








        0







        You can use the copyfile of shutil.



        import shutil

        ....< part of your code > ...
        shutil.copyfile(filename, filename.replace('.csv', '.txt'))





        share|improve this answer













        You can use the copyfile of shutil.



        import shutil

        ....< part of your code > ...
        shutil.copyfile(filename, filename.replace('.csv', '.txt'))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 11:02









        Niels HenkensNiels Henkens

        766517




        766517

















            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma