473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

os.path pathname manipulations

30
Expand|Select|Wrap|Line Numbers
  1.  
  2. for dirpath,dirs,files in os.walk(cwd):
  3. for file in files:
  4.  
  5. global z_path
  6. z_path = os.path.  # WHAT WOULD GO HERE #  (dirpath)   
  7.  
  8. print z_path
  9.  
  10.  
This is part of a script where I am tying to grab the names of folders and an undetermined number of sub-folders/files and print them to screen. I don't know how deep the sub-folders will go. Currently when I do this it prints things like

C:\Documents and Settings\B\Desktop\Test\T1\test.txt.

What I want is to go from cwd (in this case Test) and just print to screen

\T1\test.txt

I have tried many os.path pathname manipulators like...

abspath( path)
basename( path)
commonprefix( list)
dirname( path)
split( path)
splitdrive( path)
splitunc( path)

...and none seem to work.

I can't think of any way to do this with a url that will change so the url could also be...

C:\Documents and Settings\B\Desktop\Test\T1\444\test.txt.

and I want it to go from test > test.txt or T1\444\test.txt...

Ignore whats below if it doesn't make sense.
I thought of assigning a variable to cwd and then splitting the url based on that, but I don't know how to go about doing that specifically.

Does anyone know how this could be done?
Oct 14 '08 #1
1 1286
I could not exactly understand your problem.
According to my understanding you dont want the whole Filepath to be printed on the console.

Try this code

Expand|Select|Wrap|Line Numbers
  1.  
  2. Curdir = os.getcwd()
  3.  
  4. for DirInfo in os.walk(Curdir):
  5.     for File in DirInfo[2]:
  6.        print (DirInfo[0]+"\\"+File).replace(Curdir,"")
  7.  
  8.  
Nov 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
3
by: Golawala, Moiz M (GE Infrastructure) | last post by:
Hi all, What I am trying to do is write some generic code where I can get the path of the parent directory. for example if current working directory is 'Z:\\dirA\\Parent\\Child' I want to get...
1
by: Alia Khouri | last post by:
This may have been discussed ad nauseaum before, but every time I use os.path manipulations I miss something like Jason Orrendorf's path.py being in the standard library....
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
11
by: rh00667 | last post by:
hi all, i'm confused now. how i can get the full path of an application? if myapp is in a directory which belongs to PATH, argv gives me the first token of cmd line, and not the real path of...
9
by: elShazo | last post by:
Question: Program was originally developed in directory "/home/dude/ desktop/program" with subdirectores. I want the program and the files it needs to run (images, settings files, etc.) from a new...
0
by: Martin P. Hellwig | last post by:
Hello all, I had some troubles in the past how to arrange my packages and modules, because I usually don't develop my stuff in the Lib\site-packages directory I have some troubles when importing...
0
by: BruceMcF | last post by:
On Apr 16, 6:03 pm, "Aaron Gray" <ang.use...@gmail.comwrote:
6
by: Marcin201 | last post by:
Is there an built-in functionality in python to convert Windows paths to Unix paths? I am running into problems when creating data files on Windows and the running them on a Unix platform. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.