Python Forum
How to specify the destination folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to specify the destination folder
#1
Hi,
I read some comma separated data from the web, and want to write into a csv file.

I open a new CSV file write data (df) in it, but I could not be able to specify the destinated folder. How to specify
my desination folder is :"D:\myproject"
# csv new (create) file
fr=open('newcsvdata.csv','w',newlin='')
fr.write(df)
fr.close()
Reply
#2
Use a context manager to open a file.
Just give the whole destination path, to save the file somewhere.
You can use relative paths, then the current working directory is your start point.
Absolute paths begins with a slash and they are independent from current working directory.

with open('your_folder/yourfile.csv', 'w') as fd:
    # fd is the resulting file object

# fd is closed automatic when leaving the block (context manager)
To manipulate paths, you should use the pathlib.

from pathlib import Path


directory = Path('target_directory')
# task write files with 0.csv, 1.csv, 2.csv ...
for name in range(0, 10):
    name = str(name)
    destination = (directory / name).with_suffix('.csv')
    with destination.open('w') as fd:
        # code to write content
        pass
or globbing like you do it in the shell:


from pathlib import Path


directory = Path('target_directory')
for file in directory.glob('*.csv'):
    with file.open() as fd:
        # code to read content
        pass
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 532 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  python move specific files from source to destination including duplicates mg24 3 1,096 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,473 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,467 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,044 Aug-19-2020, 12:40 PM
Last Post: rdDrp
  Shutil move if file exists in destination Friend 2 6,759 Feb-02-2020, 01:45 PM
Last Post: Friend
  Delete directories in folder is not working after folder is updated asheru93 2 2,649 Feb-13-2019, 12:37 PM
Last Post: asheru93
  copy content of folder to existing folder shlomi27 0 2,638 Aug-11-2018, 01:44 PM
Last Post: shlomi27
  get destination path from item from QlistWidget faithcure 3 3,277 Jul-25-2018, 09:37 PM
Last Post: nilamo
  copy files from one destination to another by reading filename from csv Prince_Bhatia 3 7,630 Feb-27-2018, 10:56 AM
Last Post: Prince_Bhatia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020