file-handling

Python – How to read file one character at a time?

Python – How to read file one character at a time? Question: I am learning python file handling. I tried this code to read one character at a time f = open(‘test.dat’, ‘r’) while (ch=f.read(1)): print ch Why it’s not working Here is Error message C:Python27python.exe “C:/Users/X/PycharmProjects/Learning Python/01.py” File “C:/Users/X/PycharmProjects/Learning Python/01.py”, line 4 while (ch=f.read(1)): …

Total answers: 3

How to move a file in Python?

How do I move a file in Python? Question: How can I do the equivalent of mv in Python? mv "path/to/current/file.foo" "path/to/new/destination/for/file.foo" Asked By: David542 || Source Answers: os.rename(), os.replace(), or shutil.move() All employ the same syntax: import os import shutil os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo") os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo") shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo") The filename ("file.foo") must be included in both …

Total answers: 11