How do I move a text file to a different folder? [duplicate] - python

This question already has answers here:
How to move a file in Python?
(11 answers)
Closed 2 years ago.
I already know how to create and write a text file in Python, but how do I move that file to a different folder on my system?

You can either make a system call with os.system:
import os
os.system("mv /path/to/file /path/to/destination")
or rename it:
os.rename("/path/to/file", "/path/to/destination")
or move it with `shutil.move`:
```python
import shutil
shutil.move("/path/to/file", "/path/to/destination")
First solution works only in bash shells, second and third should be portable over all platforms. Third has the advantage that you can specify a folder as destination, the file will then be put into that folder with the same name as in the old location.

Related

How do I delete files from the directory using python [duplicate]

This question already has answers here:
How to delete a file by extension in Python?
(7 answers)
Closed 4 years ago.
want to delete pdf files from the directory using python.
Having "pdffiles" name folder in that lost of pdf are there So I want to delete all files from it but don't want to delete folder, want to delete just file from folder. how can I do it.(may be using os.remove())
Try listdir+remove:
import os
for i in os.listdir('directory path'):
if i.endswith('.pdf'):
os.remove(i)

How to create directory in ram and get path to it? [duplicate]

This question already has answers here:
How can I create a ramdisk in Python?
(4 answers)
Closed 3 years ago.
I'm looking for a library that allows to create a "fake" directory in RAM and get path to it that would work like a path to normal directory on disk.
My scenario is that I have a python script that executes another program (third party that I cannot modify) but it produces files and writes them to a specified location.
Then, in my script, I read these files and do something with them.
It's slow and I know that the bottleneck here is reading/writing files to/from disk (even if it's SSD).
Is it possible that I don't change the core of my script and only replace the temporary folder path that I use to store intermediate files?
I don't need them and I remove them after they are processed.
The perfect solution would be something like this:
import fakeRAM
tmp_dir_path = fakeRAM.get_path()
...
os.system("program.exe " + tmp_dir_path)
import tempfile
import pandas as pd
import io
data=pd.read_csv("data.csv")
temp = tempfile.NamedTemporaryFile(prefix="", suffix=".csv", mode='w+b', delete=False)
data.to_csv(temp.name)
print("The created file is", temp.name)

How to erase the first character from all files (on Windows) into a folder using Python 3? [duplicate]

This question already has answers here:
How to rename a file using Python
(17 answers)
Closed 6 years ago.
I have a folder of log files recorded as XML. A guy in here made a mess in there and changed the script of creation of those files and insert a 'p' letter at the beginning. E.g.:
The file name is now: p0154656450103.xml
When it should be: 0154656450103.xml
There are about 3,000 files this way. How can I rename them erasing just the first character or just the 'p' character?
You can use os.rename:
import os
os.rename(src, dst)

How to change file extension with Python? [duplicate]

This question already has answers here:
Extracting extension from filename in Python
(33 answers)
Closed 7 years ago.
I'm trying to make a program that will take a file, say my_test_file.log and make a new file called my_test_file.mdn. I'd like to be able to use this program by typing python renameprogram.py my_test_file.log into the command line. The original file will always end in .log.
from shutil import copyfile
from glob import glob
map(lambda x:copyfile(x,x[:-3]+"mdn"),glob("*.log"))
or perhaps more simply
...
import sys
copyfile(sys.argv[1],sys.argv[1][:-3]+"mdn")
You certainly can create a Python program that will accomplish this, but there are shell level commands that already do this.
For Linux/Unix:
mv my_test_file.log my_test_file.mdn
For Windows (see this link):
rename my_test_file.log my_test_file.mdn

How to find what files are in a folder [duplicate]

This question already has answers here:
Directory-tree listing in Python
(21 answers)
Closed 9 years ago.
I am writing a function that is recieving a folder path as an arguemnt. I want her to add into a dictionary what's inside the folder (like dir in CMD)
How can I do this ?
Thank you in advance,
Iliya
import os
print os.listdir('/tmp')
Similar Topics:
Directory listing in Python
Also, I use os.path and glob a lot while manipulating file system path.
You might want to check it out.

Categories