How can I read a .txt
file in python and write those contents into a .md
file without any change in content. I have googled enough but couldn't find a proper answer . ?
Asked
Active
Viewed 4,229 times
0

saruftw
- 1,104
- 2
- 14
- 37
-
1[shutil.copyfile](https://docs.python.org/2/library/shutil.html#shutil.copyfile) – inspectorG4dget May 10 '15 at 22:32
-
1Just to make it clear, you literally mean "the exact same bytes as a `.md` file instead of a `.txt` file", right? If so, then it's a dup. Probably of [How do I copy a file in Python](http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python), which has a variety of great answers with all the info you could ever want. – abarnert May 10 '15 at 22:33
-
1… except that you might also want to know about `os.path.splitext` to help you convert `"spam.txt"` to `"spam.md"` if that `"spam.txt"` is a value in a variable rather than a constant in your source. – abarnert May 10 '15 at 22:35
-
Thanks ! I have enough resource now . – saruftw May 10 '15 at 22:36
-
I was still wondering, if writing to a `.md` file directly is possible ? – saruftw May 11 '15 at 08:57
1 Answers
3
If you're not changing the contents you could just change the file extension. See this SO answer for help
Change the file extension for files in a folder in Python
Edited based on comment If you want to make a copy this is the simplest way I believe
import shutil
shutil.copyfile("testcopy.txt", "testcopy.md")
-
I want to write the contents to another file . Is there a way in python to make a copy of this file and then change its extension !? – saruftw May 10 '15 at 22:29
-
@saru95: Sure. See the [`shutil`](https://docs.python.org/3/library/shutil.html) module. – abarnert May 10 '15 at 22:32