473,757 Members | 6,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining if a filename is greater than X characters

How would I determine if a filename is greater than a certain number
of characters and then truncate it to that number? For example a file
named XXXXXXXXX.txt would become XXXXXX

fname = files
if fname[0] > 6
print fname[0]

Thanks!!!
Jul 18 '05
11 3246
hokiegal99 wrote:
One last question and then I'll leave you guys alone for awhile: How
would I append a string to the end of each file that I've truncated? I'd
like to put '.txt' on the end, but I don't understand how to go about
it. When I tried this:

old_fname = files
new_fname = old_fname.appen d('.txt')

.txt was added as a string to the files list. Researching a bit on
Google told me that in Python strings are unchangeable. So, how would I
go about changing a string?


You cannot change a given string-object, any more than you can change
a given number-object -- the object 23 will always have value 23 (never
22 nor 24 nor any other number), the object 'foo' will always have
value 'foo' (never 'bar' nor 'foobar' nor any other string).

However, you can re-bind a name to refer to a different object than
the one it previously referred to. Thus, for example:

x = 23
x = x + 1

this has not changed the number 23, whose value IS still 23, but name
x now refers to a different number, namely, the number 24. Similarly:

x = 'foo'
x = x + '.txt'

this has not changed the string 'foo', whose value IS still 'foo', but name
x now refers to a different string, namely, the string 'foo.txt'.

The unchangeability of strings doesn't inhibit string manipulation any
more than the unchangeability of numbers inhibits arithmetics. Simply,
operations on strings build and return new strings, just like operations
on numbers build and return new numbers, and in either case you may, if
you wish, re-bind some pre-existing name to refer to the new objects.
Alex

Jul 18 '05 #11
hokiegal99 wrote:
OK, I'll use spaces... I thought tabs akward, but they take less time in
my editor than spaces do. Thanks for the for loop idea. It works
perfectly. Below is the script with spaces instead of tabs. Thanks
again!!!


You're welcome! Note that good editors can be configured so as to let
you hit the Tab keys for indentation (and shift-Tab to go back, if you
wish) while using spaces in the file as saved. If your current editor
does not support that I would suggest you consider changing -- there
are SO many good editors out there, after all. For example, IDLE, the
integrated GUI development environment that comes with Python, is quite
nice now in its release 1.0 (comes with Python 2.3). The Options menu
opens a multi-tab dialog open at the first tab, "Fonts/Tabs", and you'll
see a simple choice between "Tab key inserts spaces" (default) and an
alternative of "Tab key inserts tabs". Even though I normally use VIM
myself (a highly configurable & programmable descendant of good old vi)
and occasionally SciTE (a useful lightweight editor/environment), I'm
seriously tempted by the new IDLE 1.0 to switch for much of my work...
Alex

Jul 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6596
by: Ralph Freshour | last post by:
I have a PHP web app using MySQL - when I save a .jpg file named test's.jpg I see that the filename on the unix server is: test\'s.jpg - the filename I end up saving in my SQL table is named test's.jpg - when I use an image tag to display the photo on my web page, no image displays. I tried to strip the slash out of the filename but the image still won't display on the web page - maybe I'm all goofed up here and don't understand what's...
3
20986
by: Martin Lucas-Smith | last post by:
Is there some way of using ereg to detect when certain filename extensions are supplied and to return false if so, WITHOUT using the ! operator before ereg () ? I have an API that allows as an input a regular expression, enabling the administrator to ensure a file upload matches a certain pattern. For instance, supplying the string '.exe$|.com$|.bat$|.zip$|.doc$'
0
1288
by: umesh | last post by:
Hi, I am using MicroSoft.NET Framework 1.1 Version 1.1.4322 on Japanese Windows 2000. In my application, I have given user facility to upload and download the files. If FileName is Japanese and greater than 17 characters, then the filename does not appear in file download dialog box.
2
2908
by: Xam | last post by:
Hello everybody Do you know of a javascript routine that can warn if there are any pre-defined invalid chars in the filename of an INPUT file box before it is submitted with the submit button. The process would be: a) User clicks the INPUT File's Browse button to select the file from their computer.
1
2104
by: CB | last post by:
Using C# in .Net 2003, DataSet.ReadXml fails when a percentage (%) sign is in the filename followed by 2 hex characters. Seems that the % sign is likely encoding the following 2 hex characters. So c:\test%ab.xml fails for ReadXml since %ab is interpreted as 171 and c:\test171.xml does not exist. There is no problem with c:\test%mn.xml because "mn" is not a hex code. If I replace the % sign with %25 (25hex = 37dec = ascii code for %),...
10
13076
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly generated number from a program that I saved from. So I need to find a way that I can quickly remove the underscore and the last 4 numbers from the filename without changing anything else. I can't set a max length to the file becuase the filename's...
13
7255
by: Bryan Parkoff | last post by:
I have seen that C/C++ Compiler supports long filename up to 254 characters plus the extension. Can header files and source code files accept space between alphabet character and numeric character? Is it the best practice to use underscore instead of space? If so, please explain why. Would you prefer to avoid using two double quote marks in the long filename if space is there? Without it, it would be underspace. For example:
13
4745
by: dgk | last post by:
I can't find anything in the framework that will tell me whether a filename is valid. I suppose that I can just try to open it and trap an error but that seems wasteful. I dug around and found this code: Public Function IsValidName(ByVal name As String) As Boolean Dim i As Integer For i = 0 To name.Length - 1 Dim ch As Char = name.Chars(i) Dim uc As Globalization.UnicodeCategory =
18
2774
by: RedLars | last post by:
Hi, How can check using .NET 1.1 that a string contains a valid filename for winxp? The application in question has a textbox where user can enter filename and only the filename. It should not allowed to enter path +filename like "c:\tmp\myfile.log" or relative paths "..\tmp \myfile.log" - no directory info should be allowed, only the filename.
0
9489
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.