Python - Read file names in directory, write twice to text file (one without file extensions), and separate with pipe

Yahtzee24

I have a directory (c:\temp) with some files:

a.txt  
b.py  
c.html

I need to read all of the files in a directory and output it to a text file. I've got that part handled (I think):

WD = "c:\\temp"

import glob

files = glob.glob('*.*')
with open('dirList.txt', 'w') as in_files:
    for eachfile in files: in_files.write(eachfile + '\n')

I need the output to look like:

a|a.txt  
b|b.py  
c|c.html

I'm not quite sure where to look next.

Mureinik

I'd split the file name by . and take the first part:

for eachfile in files:
    in_files.write('%s|%s\n' % (eachfile.split('.')[0], eachfile))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a command to write text to a file without redirection, pipe or function?

From Dev

Python: Write a dictionary to a text file (CSV?) and read that text file into a dictionary

From Dev

How to write out all directory names into a text file

From Dev

(OS X Shell) Copying files based on text file containing file names without extensions

From Dev

read directory & write file list to file

From Dev

Write to separate files with different file names

From Dev

Read output of a csv file and write it to a text file using Python

From Dev

Python :- Read a text file and convert it to upper case and write to a second file

From Dev

Filter by extensions and file names

From Dev

Filter by extensions and file names

From Dev

Read and Write to text file in java

From Dev

Want to write unique filenames and paths to text file, duplicate names will have different extensions

From Dev

How to Read from a text file and then Write to another in one Console application?

From Dev

How to Read from a text file and then Write to another in one Console application?

From Dev

PYTHON - How to change one character in many file names within a directory

From Dev

Open (read, write) a file in same directory as main app - python/kivy

From Dev

Read unknown file names from directory

From Dev

VBScript: Read a text file into another one without skipping last line

From Dev

Global alias for all files in a directory, by their file names (extensions, if available will be omitted)

From Dev

Getting directory names and writing them to a text file

From Dev

Read and Write .docx file with python

From Dev

Python - read, write and append to a file

From Dev

Python read and write 'ß' from file

From Dev

pselect notifies both pipe read end file descriptors even though only one write end was written on

From Dev

Python: read a text file and copy/paste directories listed in it to a new directory

From Dev

Why does python read my file twice?

From Dev

Finding repeated file names in directory (without specifying the exact file name)

From Dev

Finding repeated file names in directory (without specifying the exact file name)

From Dev

How to Read from and then Write/Append to a Text File without re-opening the file?

Related Related

  1. 1

    Is there a command to write text to a file without redirection, pipe or function?

  2. 2

    Python: Write a dictionary to a text file (CSV?) and read that text file into a dictionary

  3. 3

    How to write out all directory names into a text file

  4. 4

    (OS X Shell) Copying files based on text file containing file names without extensions

  5. 5

    read directory & write file list to file

  6. 6

    Write to separate files with different file names

  7. 7

    Read output of a csv file and write it to a text file using Python

  8. 8

    Python :- Read a text file and convert it to upper case and write to a second file

  9. 9

    Filter by extensions and file names

  10. 10

    Filter by extensions and file names

  11. 11

    Read and Write to text file in java

  12. 12

    Want to write unique filenames and paths to text file, duplicate names will have different extensions

  13. 13

    How to Read from a text file and then Write to another in one Console application?

  14. 14

    How to Read from a text file and then Write to another in one Console application?

  15. 15

    PYTHON - How to change one character in many file names within a directory

  16. 16

    Open (read, write) a file in same directory as main app - python/kivy

  17. 17

    Read unknown file names from directory

  18. 18

    VBScript: Read a text file into another one without skipping last line

  19. 19

    Global alias for all files in a directory, by their file names (extensions, if available will be omitted)

  20. 20

    Getting directory names and writing them to a text file

  21. 21

    Read and Write .docx file with python

  22. 22

    Python - read, write and append to a file

  23. 23

    Python read and write 'ß' from file

  24. 24

    pselect notifies both pipe read end file descriptors even though only one write end was written on

  25. 25

    Python: read a text file and copy/paste directories listed in it to a new directory

  26. 26

    Why does python read my file twice?

  27. 27

    Finding repeated file names in directory (without specifying the exact file name)

  28. 28

    Finding repeated file names in directory (without specifying the exact file name)

  29. 29

    How to Read from and then Write/Append to a Text File without re-opening the file?

HotTag

Archive